Forum: Ruby on Rails select :selected doesn't actually generate a selected option

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Patrick McCafferty (Guest)
on 2006-05-05 09:26
(Received via mailing list)
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 L. (Guest)
on 2006-05-05 17:19
(Received via mailing list)
>         ["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/
This topic is locked and can not be replied to.