Select :selected doesn't actually generate a selected option


#1

All,

I have code that looks like this:

<%= select(‘date’, ‘month’, [
[“1e$B7ne(B”,“1”],
[“2e$B7ne(B”,“2”],
[“3e$B7ne(B”,“3”],
[“4e$B7ne(B”,“4”],
[“5e$B7ne(B”,“5”],
[“6e$B7ne(B”,“6”],
[“7e$B7ne(B”,“7”],
[“8e$B7ne(B”,“8”],
[“9e$B7ne(B”,“9”],
[“10e$B7ne(B”,“10”],
[“11e$B7ne(B”,“11”],
[“12e$B7ne(B”,“12”]], :selected => @month, :include_blank => false ) %>
<%=
select_year(Date.new(@year, @month, 1)) %> <%= submit_tag ‘e$B9T$/e(B’
%>

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

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.


#2
    ["10e$B7ne(B","10"],
    ["11e$B7ne(B","11"],
    ["12e$B7ne(B","12"]], :selected => @month, :include_blank => false ) %> <%=

select_year(Date.new(@year, @month, 1)) %> <%= submit_tag ‘e$B9T$/e(B’ %>
<…>
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/