Ruby-on-rails 有人可以用清晰、简单的术语向我解释 collection_select 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8907867/
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
Can someone explain collection_select to me in clear, simple terms?
提问by marcamillion
I am going through the Rails API docs for collection_selectand they are god-awful.
我正在浏览 Rails API 文档collection_select,它们太糟糕了。
The heading is this:
标题是这样的:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
And this is the only sample code they give:
这是他们提供的唯一示例代码:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)
Can someone explain, using a simple association (say a Userhas_many Plans, and a Planbelongs to a User), what I want to use in the syntax and why?
有人可以解释一下,使用一个简单的关联(比如一个Userhas_many Plans,一个Plan属于 a User),我想在语法中使用什么以及为什么?
Edit 1:Also, it would be awesome if you explained how it works inside a form_helperor a regular form. Imagine you are explaining this to a web developer that understands web development, but is 'relatively new' to Rails. How would you explain it?
编辑 1:此外,如果您解释它在 aform_helper或常规表单中的工作方式,那将会很棒。想象一下,您正在向一位了解 Web 开发但对 Rails “相对较新”的 Web 开发人员解释这一点。你会怎么解释?
回答by alexkv
collection_select(
:post, # field namespace
:author_id, # field name
# result of these two params will be: <select name="post[author_id]">...
# then you should specify some collection or array of rows.
# It can be Author.where(..).order(..) or something like that.
# In your example it is:
Author.all,
# then you should specify methods for generating options
:id, # this is name of method that will be called for every row, result will be set as key
:name_with_initial, # this is name of method that will be called for every row, result will be set as value
# as a result, every option will be generated by the following rule:
# <option value=#{author.id}>#{author.name_with_initial}</option>
# 'author' is an element in the collection or array
:prompt => true # then you can specify some params. You can find them in the docs.
)
Or your example can be represented as the following code:
或者您的示例可以表示为以下代码:
<select name="post[author_id]">
<% Author.all.each do |author| %>
<option value="<%= author.id %>"><%= author.name_with_initial %></option>
<% end %>
</select>
This isn't documented in the FormBuilder, but in the FormOptionsHelper
这在 中没有记录FormBuilder,但在FormOptionsHelper
回答by zsquare
I've spent quite some time on the permutations of the select tags myself.
我自己花了很多时间在选择标签的排列上。
collection_selectbuilds a select tag from a collection of objects. Keeping this in mind,
collection_select从对象集合中构建选择标记。牢记这一点,
object: Name of the object. This is used to generate the name of the tag, and is used to generate the selected value. This can be an actual object, or a symbol - in the latter case, the instance variable of that name is looked for in the binding of the ActionController(that is, :postlooks for an instance var called @postin your controller.)
object: 对象名称。这用于生成标签的名称,并用于生成选定的值。这可以是一个实际的对象,也可以是一个符号 - 在后一种情况下,该名称的实例变量在 的绑定中ActionController:post查找(即查找@post在您的控制器中调用的实例变量。)
method: Name of the method. This is used to generate the name of the tag.. In other words, the attribute of the object you are trying to get from the select
method: 方法名称。这用于生成标签的名称.. 换句话说,您试图从选择中获取的对象的属性
collection: The collection of objects
collection: 对象集合
value_method: For each object in the collection, this method is used for value
value_method: 对于集合中的每个对象,该方法用于取值
text_method: For each object in the collection, this method is used for display text
text_method: 对于集合中的每个对象,该方法用于显示文本
Optional Parameters:
可选参数:
options: Options that you can pass. These are documented here, under the heading Options.
options: 可以通过的选项。这些记录在此处,在标题选项下。
html_options: Whatever is passed here, is simply added to the generated html tag. If you want to supply a class, id, or any other attribute, it goes here.
html_options: 无论在这里传递什么,都只是添加到生成的 html 标签中。如果你想提供一个类、id 或任何其他属性,它就在这里。
Your association could be written as:
你的关联可以写成:
collection_select(:user, :plan_ids, Plan.all, :id, :name, {:prompt => true, :multiple=>true })
collection_select(:user, :plan_ids, Plan.all, :id, :name, {:prompt => true, :multiple=>true })
With regards to using form_for, again in very simple terms, for all tags that come within the form_for, eg. f.text_field, you dont need to supply the first (object) parameter. This is taken from the form_forsyntax.
关于使用form_for,再次用非常简单的术语来说,用于 中的所有标签form_for,例如。f.text_field,您不需要提供第一个 ( object) 参数。这是从form_for语法中获取的。

