On 10 May 2012 11:45, Michael Baldock <[email protected]> wrote:
> Can anyone please tell me what's going on here, I've copied something
> pretty much directly from rails guides, but it's just not doing what
> it's supposed to!
>
> (Or I'm doing something stupid!)
>
> rails - 3.2.2
> ruby 1.9.3
>
> my view has the following
>
> 1 - <% formations_array = @formations.map { |f| [f, f] } %>
> 2 - <%= formations_array.each do |f| f.to_s end%>
> 3 - <%= f.select(:formation_csv, options_for_select(formations_array,
> 2)) %>
You are using f.select, rather than select_tag. This doesn't require
options_for_select. It will select the :formation_csv from the form's model.
I don't know what your model is, but let's say it's Team.
In your controller:
@team = Team.new
@team.formation_csv = "5-3-2"
In your view:
<%= form_for @team do |f| %>
<%= f.select(:number, formations_array) %>
<% end %>
That will select 5-3-2. If you load the model from the db, it will use the
saved value automatically.
Also, I don't think you need line 1. If you pass an array of values, Rails
will use them for both text and value, so line 1 is redundant.
Does that all make sense?
>
> 1. the array of arrays is created from the @formations
> 2. prints this array of arrays [["4-3-3", "4-3-3"], ["5-3-2", "5-3-2"],
> ["4-4-2", "4-4-2"]]
> (as I expect)
>
> 3. creates the select box, and hard coded asks for index 2 to be
> currently selected, as on rails docs here : <%=
> options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
>
> However when I look at the page, the wrong selection is selected, always
> index 0, not index 2.
>
> Can anyone please tell me what's going on here! Or is this a bug?
>
> Thanks,
>
> Michael
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.