I have this code:
%tr
%th Parent
%td
%select{ :name => "firstlevel_id", :value => "#{#s.firstlevel_id}" }
- Firstlevel.find(:all).each do |f|
%option{ :value => "#{f.id}" } #{f.title}
but even if the firstlevel_id is already one of the id's in the options list, it doesn't show it as selected.
You can use select_tag helper to create select tag and control selected value.
You can also set selected attribute on proper option tag:
%select{:name => "firstlevel_id"}
- FirstLevel.find(:all).each do |f|
%option{:value => f.id, :selected => f.id == #s.firstlevel_id} #{f.title}
I would prefer the first solution.
You should use the select_tag to generate the html , and options_for_select to generate the html :
select_tag :firstlevel_id, options_for_select( Firstlevel.all.map{|l| [l.title, l.id] }, #s.try(:firstlevel_id) )
Related
I have a separate select_tag and a link_to and I'm sending some parameters via the link_to. I want to get the selected value and send it as one of the parameters. I tried to add selected: params[:whatever] to the select_tag, that didn't work. I tried doing it via js, I can get the selected value but I'm not sure how to set to that specific parameter.
As requested:
<%= select_tag :shipping_opt, options_for_select(delivery_opt(#select_store)), :include_blank => true %>
<%= link_to 'Use this address', checkouts_step_two_path(:data => Checkout::CRYPT.encrypt_and_sign(s.id), :store => Checkout::CRYPT.encrypt_and_sign(#select_store.id), :order_info => ?? ) %>
the order_info should be the selected shipping opt.
Assuming you retrieve values from the select_tag with a format similar to this:
value = $('#selectList').val();
I'd assume that you just set this to a variable and put it into the params hash in the link_to
<%= link_to 'Use this address', checkouts_step_two_path(:data => Checkout::CRYPT.encrypt_and_sign(s.id), :store => Checkout::CRYPT.encrypt_and_sign(#select_store.id), **:order_info => value** ) %>
When submitting the below form, parameters are available in the controller with params[:teachers], but in the view (same view is rendered), selected value for "language_id" isn’t params[:teacher][:language_id], but the default one (first option).
<%= form_for :teacher , :url => {:action =>"search_teacher"} , :html => { :method => "post"} do | f | %>
<%= f.select :language_id , t('languages_hash'), :include_blank => false %>
<%= f.submit :value => t("search_button") %>
When debugging with the debug method in the view, « params[:teacher][:language_id] » parameter is present.
Thank you
By what I understood from your question, you are trying to get the previously selected language to remain selected after the form submission. For that you may need to pass a selected option in the f.select.
<%= f.select("Language", "language_id", Language.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true, :selected => params[:language_id] }) %>
This may do the trick.
For more option you can go through this link
I'm guessing that t('languages_hash') isn't returning data in the proper format that the select tag is expecting.
Check out the docs - I'm guessing you'll want to wrap that in an options_for_select call, and possibly change the order of what is returned from t('languages_hash').
I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:
View Select Field
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(#itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
Form Helper
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
I have tried making the view look like this:
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
But I get wrong number of arguments (2 for 1)) for that line. Also tried
<%= f.select :start, options_for_select(#itinerary.locations), {:selected => #newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!
Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?
You could try something like this in your helper
def options_for_select(locations, selected=nil)
locations.map do |l|
tag_options = location_option_attributes(l)
if l == selected
tag_options[:selected] = "selected"
end
content_tag "option", l.masterlocation.name, tag_options
end.join("\n")
end
then call field like you were trying before.
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
You can pass one more parameters to select the value like this:
<%= f.select :start, options_for_select(#itinerary.locations), :selected => #newsavedmap.start, {:include_blank => true}, {:id=>"startdrop" } %>
I want to keep the select_tag(:multiple => true) options to be selected which were selected by user once search is performed
<%= select_tag 'values[]', method_for_options_for_select, :class => 'some-class', :multiple => true, :size => 6 %>
Suppose a user select 4 values from the select tag then for values should be selected,
How can we pass this 4 values to the select_tag?
I tried using :selected => params['values[]'] but this doesnt works for multiple true
Any help will be appreciated
Ref this and options_for_select
Something like following
<%= select_tag 'values[]',
options_for_select(#stores.map {|s| [s.store_name, s.store_id]},
#user.stores.map {|j| j.store_id}),
:class => 'some-class', :multiple => true, :size => 6 %>
I have the following which creates a select box:
<%=select_tag "people", options_from_collection_for_select(#people, "id", "name")%>
This creates an item for each person, problem is I would like a "All People" value 0, option added and selected by default on load?
does the select_tag in rails 3 support this?
Thanks
Simply include a :include_blank => 'All People' option in your select_tag:
<%= select_tag "people", options_from_collection_for_select(#people, "id", "name"), :include_blank => 'All People' %>