Ruby-on-rails 从复选框构建数组时 form_for 的语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8850188/
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
Syntax for form_for when building an array from checkboxes
提问by Max Williams
I'm making a form for an Order object, and the order has many Products, via a join table called OrderProducts. So, we've got something like this:
我正在为 Order 对象创建一个表单,并且该订单通过名为 OrderProducts 的连接表包含许多产品。所以,我们有这样的事情:
<% @order = Order.new %>
<% form_for @order do |f| %>
<% @products.each do |product| %>
... want to iterate over products here to build up "order[product_ids][]", with one checkbox per product
<% end %>
<% end %>
Usually for each product i would have a check_box_tag, saying
通常对于每个产品,我都会有一个 check_box_tag,说
<%= check_box_tag "order[product_ids][]", product.id, @order.product_ids.include?(product.id) %>
But this, while working fine, always feels like a bit of a cop out. Is there a way i can do it with the f.check_boxsyntax? Important note - on the project in question I'm working in Rails 2.2.2, so a solution that works in rails 2 would be ideal.
但这虽然工作正常,但总感觉有点像警察。有没有办法用f.check_box语法来做到这一点?重要说明 - 关于我在 Rails 2.2.2 中工作的相关项目,因此适用于 rails 2 的解决方案将是理想的。
回答by Brad Werth
Rails <= 2.x(original)
Rails <= 2.x(原始)
<% @products.each do |product| -%>
<% fields_for 'product[]' , product do |product_fields| -%>
[...]
<%= product_fields.check_box :id %>
<% end -%>
<% end -%>
Rails >= 3.x(updated)
Rails >= 3.x(更新)
<% @products.each do |product| -%>
<%= fields_for 'product[]' , product do |product_fields| -%>
[...]
<%= product_fields.check_box :id %>
<% end -%>
<% end -%>
回答by Carl
I know the author was looking for version 2 answers, but this is the top hit for google and I though I would update:
我知道作者正在寻找第 2 版的答案,但这是 google 的热门话题,虽然我会更新:
One can do this ( I'm using 4.0, don't know how far back it goes ):
可以做到这一点(我使用的是 4.0,不知道它可以追溯到多远):
<%= form_for @order do |form| %>
<%= form.collection_check_boxes(:product_ids, Product.all, :id, :labeling_method ) %>
<% end %>
For more info: http://edgeapi.rubyonrails.org...
回答by gtd
I've done a number of multi checkbox forms over the years and different Rails version. Rails has never provided any really clean way to do it, but the "cop out" solution you came up with is pretty good isn't it? It's one line, it's explicit, and as long as the list of products is reasonably short it will perform adequately.
多年来,我已经完成了许多多复选框表单和不同的 Rails 版本。Rails 从来没有提供任何真正干净的方法来做到这一点,但是您提出的“逃避”解决方案非常好,不是吗?这是一行,它是明确的,只要产品列表相当短,它就可以充分发挥作用。
To answer your specific question, f.check_boxwill never work for this. It's just short hand for the check_box_tag, but none of the semantics apply. If you want to go Rails native, the only possibility I think is to use nested attributes. Part of the problem is that there is not one obvious way for this type of thing to work. Rails core went through a lot of planning and feedback to come up with nested attributes as they exist, and though they seem a bit obtuse, they capture the most common use cases quite elegantly. But nested attributes were introduced in Rails 2.3, and besides they will introduce quite a bit of conceptual overhead for something which sounds like it doesn't need the complexity.
要回答您的具体问题,f.check_box永远不会为此工作。它只是 check_box_tag 的简写,但没有任何语义适用。如果您想使用 Rails 本机,我认为唯一的可能性是使用嵌套属性。部分问题在于,这种类型的事情没有一种明显的工作方式。Rails 核心经历了大量的规划和反馈,以提出存在的嵌套属性,尽管它们看起来有点迟钝,但它们非常优雅地捕获了最常见的用例。但是嵌套属性是在 Rails 2.3 中引入的,除此之外,它们还会为一些听起来不需要复杂性的东西引入相当多的概念开销。
There are also some plugins that provide helpers for this, although I haven't used any in a long time (since Rails 2 era actually). My impression is that they too are overkill unless you have many forms that make use of this pattern.
还有一些插件为此提供了帮助,尽管我很长时间没有使用过任何插件(实际上是从 Rails 2 时代开始)。我的印象是,除非您有许多使用这种模式的表单,否则它们也太过分了。
In short, I think you should go ahead with your existing solution.
简而言之,我认为您应该继续使用现有的解决方案。
回答by Matsumoto Kazuya
check_boxes option is very good to implement multiple checkboxes
check_boxes 选项非常好实现多个复选框
like
喜欢
f.input :yourcolumn, :as => :check_boxes, :collection => your_collection

