How to pre-select in options_from_collection_for_select? - ruby-on-rails

I have the following form:
<%= form_tag users_path, method: :get, id: 'uco' do %>
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name'), :include_blank => true %>
<%= submit_tag "Search" %>
<% end %>
When I submit the form I end up with:
www.example.com/users?country=US
I would then like the form to pre-select params[:country].
However I do not know how to attach params[:country] into the select_tag. I unsuccessfully tried:
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name', params[:country]), :include_blank => true %>
based off of this example from the apidock.

Nevermind, I solved it with this answer:
How to make the select_tag keep value of last search?
:selected => params[:country]

Related

Submitting multiple attributes to controller from collection select in form

I want a collection select tag in rails to submit two attributes of the chosen item to the controller when the form is submitted. Basically, I have a list of counties and I want to submit both the county and the state as parameters. No problem having it submit one or the other, but not both. Am I thinking about this the wrong way? Here's what I have so far...
<%= form_tag(plans_path, method: 'get', action: 'screen2') do %>
<%= text_field_tag :ZIP, "ZIP Code", id: "zipBlur"%>
<%= collection_select(nil, :county, #counties.order('RES_RATIO DESC'), :COUNTY, :COUNTY_NAME, {:selected => "#{params[:county]}"}) %>
<%= submit_tag 'Screen', :name=> nil %>
<% end %>
Thanks for your help!
using :multiple => true
ex:
<%= collection_select(:ingredient, :supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name, {:selected => #ingredient.supplier_ids, :include_blank => true}, {:multiple => true}) %>

if statement based on form select rails

I have a basic form:
<%=form_for #person do |f| %>
<%= f.label :ThingsWanted, "Stuff I need" %>
<%= f.select :ThingsWanted, ['cat', 'dog', 'purse', 'lipstick', 'fish'], { :include_blank => 'Selector' }, :required => true %>
<%= f.submit 'Done'%>
<%end%>
What I want to do is created an if statement based on what is selected before the form is submitted. Say lipstick is selected
Then I want to create a new form value called explain_yourself
<%if lipstick is selected%>
explain_yourself goes here
<%end%>
I don't know how to create the if-statement or rather access the select value before the form is submitted...
You can use jquery.
Add a class to the form and also to the input tag in question in the HTML
<%=form_for #person do |f| :class => "the_form_class" %>
<%= f.select :ThingsWanted, ['cat', 'dog', 'purse', 'lipstick', 'fish'], { :include_blank => 'Selector' }, :class => "the_input_class" :required => true %>
then in jquery, add an on-submit function to the form
$(".the_form_class").on("submit", some_function)
note: make sure you wrap this on-submit event handler in a document.ready
then define that function (the definition does not need to be wrapped in a document.ready)
function some_function(e){
e.preventDefault();
if( $(".the_input_class").val() == "lipstick"){
do_some_stuff
}
$(".the_form_class").submit();
}
This best workaround I found for this was using parameters and instance variables. Sorry the example is not great, but it works.
Do you have lipstick?
<%= link_to "Yes", onclick: #a=0%></td>
<%= link_to "No", onclick: #a=1%>
<% if params[:onclick].to_i == 1%>
<%= f.label :ThingsWanted, "Stuff I need" %>
<%= f.select :ThingsWanted, ['lipstick'], { :include_blank => 'Selector' }, :class => "the_input_class" :required => true %>
<%else%>
<br/>
<%= f.label :explain_yoself, "Select what you need" %>
<%= f.select :ThingsWanted, ['cat', 'dog', 'purse', 'fish'], { :include_blank => 'Selector' }, :required => true %>
<%= f.text_area :explain_yourself %>
<%end%>

Rails: How do I set up a collection_select?

I'm trying to set up a search / filter using collection_select.
Firstly: This works. It lists all cases for employee with id = 15.
<%= form_tag(cases_path, :method => "get") do %>
<%= hidden_field_tag :param_e, 15 %>
<%= submit_tag "Filter", :name => nil %>
<% end %>
But what I want is a collection_select, so I can list cases for any employee.
<%= form_tag(cases_path, :method => "get") do %>
<%= collection_select( :x, :y, Employee.all, :id, :name, {}, { :multiple => false }) %>
<%= hidden_field_tag :param_e, :z %>
<%= submit_tag "Filter", :name => nil %>
<% end %>
This shows the collection_select with all the employees in a drop-down.
How to I connect-up the collection_select?

Rails: Getting a Value from a Select Tag

<%= form_tag 'select_domain', :url => administer_admin_domain_path(:id), :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange => "this.form.submit();" %>
<% end %>
I want the option selected's id to be the :id inside my form's url when it submits, is this possible?
How about:
<%= form_tag 'select_domain', :url => administer_admin_domains_path, :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange=>"this.form.action=this.form.action + '/' + this.value; this.form.submit(); %>
<% end %>
I didn't test the code. Just giving u some idea which might help. : )

Submitting a Form from index page in Rails

I'm Rails newbie.
How to make a form which allows user to choose a language(en,fr etc) through Radio buttons in Home#Index View to Submit to Home#Language action ?
Thanks in advance
<%= form_tag language_path, :method => :post do %>
<%= label_tag :language_english, 'English' %>
<%= radio_button_tag :language, 'english' %>
<%= label_tag :language_french, 'French' %>
<%= radio_button_tag :language, 'french' %>
<%= submit_tag %>
<% end %>
Where language_path is the path defined in your routes.rb, such as
match "/home/language" => "home#language", :as => 'language'

Resources