[英]Rails3 update existing record, create new simultaneously
Quote model has a number of variables. 报价模型具有许多变量。 A quote can be added to a Cart, as a LineItem.
可以将报价作为LineItem添加到购物车。 Quote has_many :line_items,
引用has_many:line_items,
LineItem
belongs_to :quote
accepts_nested_attributes_for :quote
The idea is that if a Quote is added to the cart, its status changes (:final => true) quote_controller in its show action instantiates 这个想法是,如果将报价添加到购物车中,则其状态更改(:final => true)在其show操作实例中的quote_controller
@line_item = LineItem.new
The Quote's show view includes a form which needs to do two things simultaneously: Quote的show视图包括一个需要同时做两件事的表单:
<%= f.hidden_field :quote, :value => @quote.id %>
<%= f.hidden_field :quote, :value => @quote.id %>
The second element is the issue 第二个要素是问题
<%= form_for(@line_item) do |f| %>
[...]
<%= fields_for @line_item.quote do |quote_fields| %>
<%= quote_fields.hidden_field :final, :value => true %>
<% end %>
<% end %>
This returns the error undefined method 'model_name' for NilClass:Class
. 这
undefined method 'model_name' for NilClass:Class
返回错误的undefined method 'model_name' for NilClass:Class
。 Odd though, the context is already that model. 奇怪的是,上下文已经是那个模型。
The question was a garden path. 问题是一条花园小径。 The mistake is
错误是
A quote can be added to a Cart, as a LineItem.
No need for the LineItem. 不需要LineItem。 Quote can belong to a Cart.
报价可以属于购物车。 The assignment of cart_id does the trick.
cart_id的分配可以解决问题。 Then the show view includes a form to edit that same record
然后,显示视图包括一个用于编辑同一记录的表单
<%= form_for(@quote) do |f| %>
<%= f.hidden_field :cart_id, :value => @cart.id %>
The cart then contains all its quotes and whatever it needs. 然后,购物车包含所有报价以及所需的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.