habtm 和嵌套形式结果为零
我有 3 个型号:产品、变体和颜色。我正在使用nested_form gem。
Product has_many :variations
Variation belongs_to :product
Variation has_and_belongs_to_many :colors
Color has_and_belongs_to_many :variations
通过产品形式,我有用于变体的nested_form。我想通过复选框关联颜色,但收到 未定义的局部变量或方法“color_ids”
产品模型
def new
@product = Product.new
1.times { @product.variations.build }
end
def create
@product = Product.new(params[:product])
...
end
我的表单 //edited//
<%= nested_form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="inline-form">
<%= f.fields_for :variations %>
<p><%= f.link_to_add "Add a variation", :variations %></p>
</div>
<div class="actions">
<%= submit_or_cancel(f) %>
</div>
</div>
<% end %>
嵌套表单是一个基本表
<table id="new_item">
<tr>
<th>Name</th>
<th>Color</th>
</tr>
<tr>
<td><%= f.text_field :name, :size => 40 %></td>
<td><% for color in Color.all %>
<%= check_box_tag 'variation[color_ids][]', color.id, variation.color_ids.include?(color.id), :id => dom_id(color) %><%= label_tag dom_id(color), color.name, :class => "check_box_label" %>
<% end %>
</td>
</tr>
</table>
I have 3 models, Product, Variation and Color. I'm using the nested_form gem.
Product has_many :variations
Variation belongs_to :product
Variation has_and_belongs_to_many :colors
Color has_and_belongs_to_many :variations
Through Product form I have nested_form for Variations. I want to associate colors through checkbox but receive undefined local variable or method "color_ids"
Product model
def new
@product = Product.new
1.times { @product.variations.build }
end
def create
@product = Product.new(params[:product])
...
end
My form //edited//
<%= nested_form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="inline-form">
<%= f.fields_for :variations %>
<p><%= f.link_to_add "Add a variation", :variations %></p>
</div>
<div class="actions">
<%= submit_or_cancel(f) %>
</div>
</div>
<% end %>
And nested form is a basic table with
<table id="new_item">
<tr>
<th>Name</th>
<th>Color</th>
</tr>
<tr>
<td><%= f.text_field :name, :size => 40 %></td>
<td><% for color in Color.all %>
<%= check_box_tag 'variation[color_ids][]', color.id, variation.color_ids.include?(color.id), :id => dom_id(color) %><%= label_tag dom_id(color), color.name, :class => "check_box_label" %>
<% end %>
</td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您的问题是表单中执行
color_ids.include?(color.id)
的部分。我需要查看您的 erb 表格的其余部分来告诉您如何修复它。它类似于
variation.color_ids
。另一件需要注意的事情是,这种 for 循环风格在典型/惯用的 ruby 中很奇怪。
这是比较典型的:
I'm guessing that your issue is the part of the form doing
color_ids.include?(color.id)
. I'd need to see the rest of your form erb to tell you how to fix it.It's going to be something like
variation.color_ids
.Another thing to note, that style of for loop is odd to see in typical/idiomatic ruby.
This is more typical: