Ruby on Rails Collection select - how to pre-selec

2020-06-17 05:36发布

I spent the last three days working on the collection _ select form helper for my "listing" - form, where users can select a category.

I would like to have the category currently set in listing.category_id as the preselected value.

My view code looks like this:

<%= l.collection_select(:category_id, @category, :id, :name, options = {},
                        html_options = {:size => 10, :selected => @listing.category_id.to_s})%>

I know this is not correct, but even reading looking at the explanation from Shiningthrough (http://shiningthrough.co.uk/blog/show/6) I can not understand how to proceed.

Thank you for your support,

Michael

View: as above
Controller:

def categories #Step 2
@listing = Listing.find(params[:listing_id])
@seller = Seller.find(@listing.seller_id)
@category = 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

2条回答
狗以群分
2楼-- · 2020-06-17 06:02

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.

Let me show you an example. Assuming each post belongs to a category.

@post = Post.new

<% form_for @post do |f| %>
  <!-- no option selected -->
  <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true  %>
<% end %>

@post = Post.new(:category_id => 5)

<% form_for @post do |f| %>
  <!-- option with id == 5 is selected -->
  <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true  %>
<% end %>

EDIT:

I'd suggest to use representative variable names. Use @categories instead of @category. :) Also, split the update logic from the read-only view.

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

<% form_for @listing do |f| %>
  <%= f.collection_select :category_id, @categories, :id, :name, :prompt => true %>
<% end %>

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.

EDIT 2:

class Category
  def id_as_string
    id.to_s
  end
end

<%= f.collection_select :category_id, Category.all, :id_as_string, :name, :prompt => true  %>
查看更多
我想做一个坏孩纸
3楼-- · 2020-06-17 06:13

My category_id is saved as a string in the database, but the comparison is between integer values.

if @listing.category_id != "" 
@listing.category_id = @listing.category_id.to_i
end

This solves it - the correct value is now pre-selected.

查看更多
登录 后发表回答