Accepts_nested_attributes 不保存任何更改

发布于 2024-10-06 12:07:20 字数 1569 浏览 8 评论 0原文

也许我遗漏了一些明显的东西(希望如此),但是我在以嵌套形式保存记录时遇到了一个奇怪的问题。这是一个非常基本的设置,有一点复杂,因为我的 LineItem 模型是两个词的关系 (:line_items)。不过,我遵循了 Rails 指南,并且似乎工作正常。

我的装置正在 line_items 和发票之间创建正确的关系,并且所有内容都在我的视图中正确显示,但我无法正确保存任何 line_item 记录(在我的 Rails 控制台或我的视图中)。

class Invoice < ActiveRecord::Base
  attr_accessible :line_items  #and the rest of my relevant attributes
  has_many :line_items, :dependent => :destroy
  accepts_nested_attributes_for :line_items, :allow_destroy => true
  # Rest of my model code
end

class LineItem < ActiveRecord::Base
  attr_accessible :invoice_id  #and the rest of my relevant attributes
  belongs_to :invoice  
end

我的发票存在 line_items_attributes= 方法,但它不会为新发票保存任何 line_items。更令人恼火的是,我可以编辑现有的 line_items 或在事后分配它们,但不能一下子完成(嵌套属性的全部意义?)。我的视图甚至无法通过发票表单编辑现有的 line_items。有什么想法吗?很高兴发布更多代码,但为了简洁起见没有发布。

提前致谢...

查看代码(根据要求):(

发票部分表格)

<%= form_for(@invoice) do |f| %>
  <% @invoice.line_items.build unless @invoice.line_items.any? %>
  ...
  <% f.fields_for :line_items do |builder| %>
    <%= render 'line_item_fields', :f => builder %>
  <% end %>

(行项目部分表格)

...
<%= f.collection_select :sku_id, @skus, :id, :name, :prompt => true %>
<%= f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") %>

(javascript)

function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();
}

Maybe I'm missing something obvious (hopefully), but I'm encountering a weird problem saving records in a nested form. It's a pretty basic setup, with a minor complication in that my LineItem model is a two-word relationship (:line_items). However, I've followed Rails guidelines and it seems to be working OK.

My fixtures are creating the proper relationships between line_items and invoices, and everything is showing up properly in my views, but I can't get any line_item records to save correctly (in my Rails console or my views).

class Invoice < ActiveRecord::Base
  attr_accessible :line_items  #and the rest of my relevant attributes
  has_many :line_items, :dependent => :destroy
  accepts_nested_attributes_for :line_items, :allow_destroy => true
  # Rest of my model code
end

class LineItem < ActiveRecord::Base
  attr_accessible :invoice_id  #and the rest of my relevant attributes
  belongs_to :invoice  
end

The line_items_attributes= method exists for my Invoices, but it doesn't save any line_items for new invoices. More irritating, I can edit existing line_items or assign them after the fact, but not in one fell swoop (the whole point of nested attributes?). My views can't even edit existing line_items through the invoice form. Any ideas? Happy to post more code, but didn't for sake of brevity.

Thanks in advance...

VIEW CODE (by request):

(Form Partial for Invoices)

<%= form_for(@invoice) do |f| %>
  <% @invoice.line_items.build unless @invoice.line_items.any? %>
  ...
  <% f.fields_for :line_items do |builder| %>
    <%= render 'line_item_fields', :f => builder %>
  <% end %>

(Form Partial for Line Items)

...
<%= f.collection_select :sku_id, @skus, :id, :name, :prompt => true %>
<%= f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") %>

(javascript)

function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柠檬色的秋千 2024-10-13 12:07:20

这里可能的罪魁祸首是 attr_accessible。当您使用accepts_nested_attributes_for时,关联的属性名称为association_attributes。所以你想要

attr_accessible :line_items_attributes

而不是

attr_accessible :line_items

The likely culprit here is attr_accessible. When you use accepts_nested_attributes_for, the name of the attribute for an association is association_attributes. So you want

attr_accessible :line_items_attributes

instead of

attr_accessible :line_items
月寒剑心 2024-10-13 12:07:20

请列出您的视图代码,因为您的错误可能在于您调用嵌套表单的方式。这是我关于嵌套属性的入门知识,如果有帮助的话:

http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/

遵循本指南可能会解决您的问题。

Please list your view code, because your bug may be in how you're calling your nested forms. Here's my primer on nested attributes, if it helps:

http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/

Following this guide may take care of your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文