habtm 和嵌套形式结果为零

发布于 2024-12-17 07:47:11 字数 1789 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2024-12-24 07:47:11

我猜您的问题是表单中执行 color_ids.include?(color.id) 的部分。我需要查看您的 erb 表格的其余部分来告诉您如何修复它。

它类似于 variation.color_ids

另一件需要注意的事情是,这种 for 循环风格在典型/惯用的 ruby​​ 中很奇怪。

这是比较典型的:

<% Color.all.each do |color| %>
  <%= check_box_tag 'variation[color_ids][]', color.id, color_ids.include?(color.id), :id => dom_id(color) %><%= label_tag dom_id(color), color.name, :class => "check_box_label" %>
<% end %> 

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:

<% Color.all.each do |color| %>
  <%= check_box_tag 'variation[color_ids][]', color.id, color_ids.include?(color.id), :id => dom_id(color) %><%= label_tag dom_id(color), color.name, :class => "check_box_label" %>
<% end %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文