Ruby on Rails Collection 选择 - 如何预先选择正确的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1065318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails Collection select - how to pre-select the right value?
提问by Michael Schmitz
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.
我想将当前在listing.category_id 中设置的类别作为预选值。
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.
我知道这是不正确的,但即使阅读 Shiningthrough ( http://shiningthrough.co.uk/blog/show/6)的解释,我也无法理解如何进行。
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
回答by Simone Carletti
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.
collection_select 不支持 selected 选项,实际上它不需要它。它会自动选择其值与表单构建器对象的值相匹配的选项。
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.
我建议使用具有代表性的变量名称。使用@categories 而不是@category。:) 另外,从只读视图中拆分更新逻辑。
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.
如果它不起作用(即选择提示),则意味着您没有与该记录关联的 category_id 或 Category 集合为空。在将对象传递给表单之前,请确保不要在某处为 @listing 重置 category_id 的值。
EDIT 2:
编辑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 %>
回答by Michael Schmitz
My category_id is saved as a string in the database, but the comparison is between integer values.
我的 category_id 在数据库中保存为字符串,但比较是在整数值之间。
if @listing.category_id != ""
@listing.category_id = @listing.category_id.to_i
end
This solves it - the correct value is now pre-selected.
这解决了它 - 现在预先选择了正确的值。

