Patrick McCafferty
2006-May-05 05:24 UTC
[Rails] select :selected doesn''t actually generate a selected option
All, I have code that looks like this: <%= select(''date'', ''month'', [ ["1?","1"], ["2?","2"], ["3?","3"], ["4?","4"], ["5?","5"], ["6?","6"], ["7?","7"], ["8?","8"], ["9?","9"], ["10?","10"], ["11?","11"], ["12?","12"]], :selected => @month, :include_blank => false ) %> <%select_year(Date.new(@year, @month, 1)) %> <%= submit_tag ''??'' %> where @month is the number of the current calendar month. I''ve tried it with to_s if that was the problem, I''ve tried just :selected => 5, or :selected => "5", but nothing affects it - there is never an <option selected> in the generated html. Is it broken, is it just because I''m using utf8 strings (doubtful) or am I just doing something horribly wrong? Thanks.
Rimantas Liubertas
2006-May-05 13:16 UTC
[Rails] select :selected doesn''t actually generate a selected option
> <%= select(''date'', ''month'', [ > ["1?","1"], > ["2?","2"], > ["3?","3"], > ["4?","4"], > ["5?","5"], > ["6?","6"], > ["7?","7"], > ["8?","8"], > ["9?","9"], > ["10?","10"], > ["11?","11"], > ["12?","12"]], :selected => @month, :include_blank => false ) %> <%> select_year(Date.new(@year, @month, 1)) %> <%= submit_tag ''??'' %><...>> Is it broken, is it just because I''m using utf8 strings (doubtful) or > am I just doing something horribly wrong?Hi, you may use either :selected => @month.to_s, or have your pairs in this form: ["1?", 1]. Both work for me, but I''d go with: <%= select(''date'', ''month'', (1..12).to_a.map { |m| ["#{m}?",m]}, :selected => @month, :include_blank => false ) %> Regards, Rimantas -- http://rimantas.com/
Maybe Matching Threads
- How to give onchange for select_year rails helper....
- Specify HTML options on select_year
- Problem with date_select after Rails 2.1 upgrade
- Overriding date_select in local project to use custom value rather than blank for starting option...
- Pagination with letter (A B C D ... Z)