Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>collection_select doesn't support the selected option, in fact, it doesn't need it. It automatically selects the option whose value matches the value of the form builder object.</p> <p>Let me show you an example. Assuming each post belongs to a category.</p> <pre><code>@post = Post.new &lt;% form_for @post do |f| %&gt; &lt;!-- no option selected --&gt; &lt;%= f.collection_select :category_id, Category.all, :id, :name, :prompt =&gt; true %&gt; &lt;% end %&gt; @post = Post.new(:category_id =&gt; 5) &lt;% form_for @post do |f| %&gt; &lt;!-- option with id == 5 is selected --&gt; &lt;%= f.collection_select :category_id, Category.all, :id, :name, :prompt =&gt; true %&gt; &lt;% end %&gt; </code></pre> <p><strong>EDIT</strong>:</p> <p>I'd suggest to use representative variable names. Use @categories instead of @category. :) Also, split the update logic from the read-only view. </p> <pre><code>def categories #Step 2 @listing = Listing.find(params[:listing_id]) @seller = Seller.find(@listing.seller_id) @categories = Category.find(:all) @listing.complete = "step1" respond_to do |format| if @listing.update_attributes(params[:listing]) flash[:notice] = 'Step one succesful. Item saved.' format.html #categories.html.erb end end end &lt;% form_for @listing do |f| %&gt; &lt;%= f.collection_select :category_id, @categories, :id, :name, :prompt =&gt; true %&gt; &lt;% end %&gt; </code></pre> <p>If it doesn't work (that is it selects the prompt) it means either you don't have a category_id associated to that record or the Category collection is empty. Be sure to not reset the value of category_id for @listing somewhere before the object is passed to the form.</p> <p><strong>EDIT 2:</strong></p> <pre><code>class Category def id_as_string id.to_s end end &lt;%= f.collection_select :category_id, Category.all, :id_as_string, :name, :prompt =&gt; true %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload