Rails: Preselect a value in ActionView-Helper 'collection_select'
Asked Answered
G

5

6

I'm trying to get the ActionView-Helper collection_select to take a value that will be preselected in the dropdown-menu.

Neither (:selected in the html-option-hash)

<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %>

nor (:selected in the option-hash)

<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {:selected => @my_collection_object.id}, {}) %>

seem to work.

What am I doing wrong? Can anyone help me on this one?

Grigsby answered 19/6, 2009 at 16:19 Comment(0)
H
8

From the docs:

Sample usage (selecting the associated Author for an instance of Post, @post):

collection_select(:post, :author_id, Author.all, :id, :name_with_initial)

If @post.author_id is already 1, this would return:

<select name="post[author_id]">
  <option value="">Please select</option>
  <option value="1" selected="selected">D. Heinemeier Hansson</option>
  <option value="2">D. Thomas</option>
  <option value="3">M. Clark</option>
</select>

So you just need to make sure that @my_object.my_method returns a value that matches one of the available option values. If there's a match then that option will be selected.

Headrace answered 19/6, 2009 at 17:29 Comment(2)
I accepted this answer, though I just noticed that I had to switch from collection_select to select. I had two collection selects to choose two different "post.author_id" (using the example above) so I can compare them on the page. I had a javascript-observer on each of those collection_selects and it wouldn't work because collection_selects always outputs the following html-element-id: post_author_id. My implementation doesn't work because the html-element-id of the two collection_select is identical.Grigsby
Yes, the fact that Rails doesn't guarantee unique elements IDs is a bit of a problem.Headrace
A
2

According to the docs, if @my_object.my_method has the same value as one of the options, that one will be selected by default.

Conversely, you could try using options_from_collection_for_select in conjunction with select_tag:

<%= select_tag 'my_object[my_method]', options_from_collection_for_select(@my_collection, :id, :description_string, @my_collection_object.id) %>
Ametropia answered 19/6, 2009 at 16:35 Comment(0)
F
2

Use :selected_value

%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected_value => @my_collection_object.id}) %>
Ferreira answered 24/8, 2009 at 18:32 Comment(0)
M
0

Check if @my_object.my_method returns nil. If it does,

If calling method returns nil, no selection is made without including :prompt or :include_blank in the options hash.

Other than that, you can try using lambda, like in the rdoc example

{:disabled => lambda {|category| category.archived? }

In your case this will look like

{:selected => lambda {|obj| obj.id == @my_collection_object.id }}
Measurable answered 19/6, 2009 at 16:40 Comment(0)
S
-1

It also works if your collection is in the form of an array of 2-value arrays:

CURRENCIES = [["USD", "$"], ["BRL", "R$"]]

<%= collection_select :thing, :currency, CURRENCIES, :first, :last %>

The :first and :last trick works quite nicely for these things, without having to define a separate model for them.

Sunbow answered 17/7, 2011 at 7:59 Comment(1)
Doesn't answer the question about how to select a certain valuePinnatifid

© 2022 - 2024 — McMap. All rights reserved.