Rails select_tag selected value

In the code below, I would like to save the selected field with the passed value.

But this does not work:

@yrs =[2011,2010,2009,2008] <%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,:selected=>2011) %> 

Please advise me how to do this.

thank

+46
ruby-on-rails
Sep 08 2018-11-11T00:
source share
3 answers

Remove the part :selected=> .

Syntax:

 options_for_select(@options, @selected_options) 

Application:

 options_for_select(1..5, 3) # creates a range 1..5 , with 3 as selected by default 
+82
Feb 19 '12 at 20:31
source share
 <%= select_tag "page_type", options_for_select(@page_type.collect{ |u| [u.data_name, u.id]}, :selected=>@page.page_type), {:class =>"select_combobox",:onchange=>"reset_form(this.id,'page_type_msg');"} %> 

it works for me :)

+25
May 03 '13 at 6:37
source share

Just to clarify @M Tariq Aziz's answer:

Your code should look like this:

 @yrs =[2011,2010,2009,2008] <%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,2011) %> 

The general format for the selected tag:

 <%= select_tag 'year', options_for_select(:collection, :selected) %> 
+5
Mar 22 '12 at 16:34
source share



All Articles