views:

12

answers:

1

I have this link in my code:

link_to "New question", new_question_url(:category_id => @category.id)

I have this code in my new question form:

<p>
  <%= f.label :category_id %><br />
  <%= f.collection_select :category_id, Category.all, :id, :name %>
</p>

How can I make Rails automatically select the category from the category_id querystring item, so it is the default one in the collection_select?

Thanks.

+1  A: 

Your need to pass in a reference to the current object like this:

<%= f.collection_select(:your_object, :category_id, Category.all, :id, :name) %>

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})

"The value returned from calling method on the instance object will be selected. If calling method returns nil, no selection is made without including :prompt or :include_blank in the options hash."

See here:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

nicholasklick