我的目标是使用相同的表单创建新的Order
和关联的OrderItem
。
模型
class Order < ActiveRecord::Base
belongs_to :user
has_many :order_items, dependent: :destroy
accepts_nested_attributes_for :order_items
validates_associated :order_items
end
class OrderItem < ActiveRecord::Base
belongs_to :order
default_scope -> { order(created_at: :desc) }
end
查看
<% @items.each do |item| %>
<%= form_for(@order) do |f| %>
<%= f.hidden_field :user_id, :value => session[:user_id] %>
<%= f.fields_for :order_items do |oi| %>
<%= oi.hidden_field :product_id, :value => item.id %>
<%= oi.hidden_field :price, :value => item.price %>
<%= oi.number_field :quantity, value: 1, class: 'form-control', min: 1 %>
<% end %>
<%= f.submit "Buy Now", class: "btn btn-primary" %>
<% end %>
控制器
def new
@order = Order.new
@order.order_items.build
end
def create
@order = Order.new(order_params)
if @order.save
redirect_to cart_path
else
redirect_to root_url
end
end
private
def order_params
params.require(:order).permit(:user_id, :custom_item_id, order_items_attributes: [:product_id, :price, :quantity])
end
将嵌套表单数据提交到数据库时,会返回错误消息Unpermitted parameter: order_item
,并且只保存order
数据。
更新&lt; - 已解决此问题
当我删除“f。”来自<%= f.fields_for
的表单正确呈现,order_params
包含order_items
数据。这很有趣,因为表单助手的RailsGuide包含“f”。 http://guides.rubyonrails.org/form_helpers.html#nested-forms
参数
{"utf8"=>"✓", "authenticity_token"=>"<TOKEN>", "order"=>{"user_id"=>"1", "order_item"=>{"product_id"=>"5", "price"=>"120.0", "quantity"=>"1"}}, "commit"=>"Buy Now"}
数据仍然无法保存到相应的型号中。
更新2 &lt; - 已解决此问题
将控制器中的create
操作更新为if @order.save!
,以下是错误消息:
Validation failed: Order items order can't be blank, Order items product can't be blank, Order items quantity can't be blank, Order items price can't be blank, Order items is invalid
我认为错误出现在这行代码@order.order_items.build(order_params[:order_items_attributes])
中,但我不确定我需要改变什么。
更新3 Unpermitted parameter: order_item
错误消息
从终端:
Processing by OrdersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=><TOKEN>, "order"=> "user_id"=>"1", "order_item"=>{"product_id"=>"5", "price"=>"120.0", quantity"=>"1"}}, "commit"=>"Buy Now"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Unpermitted parameter: order_item
(0.2ms) begin transaction
SQL (1.4ms) INSERT INTO "orders" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) [["user_id", 1], ["created_at", "2016-03-18 14:58:21.724246"], ["updated_at", "2016-03-18 14:58:21.724246"]]
(14.4ms) commit transaction
order_items
数据未保存。
答案 0 :(得分:1)
首先,在OrderItem中,您的关联似乎不正确。它应该是belongs_to :order
而不是belongs_to :order_item_params
。
其次,我认为您的表单应该说<%= f.fields_for :order_items do |oi| %>
(:order_items
而非:order_item
)
最后,您不需要在控制器中执行此操作:@order.order_items.build(order_params[:order_items_attributes])
def create
@order = Order.new(order_params)
if @order.save
redirect_to cart_path
else
redirect_to root#url
end
end
def order_params
params.require(:order).permit(:user_id, :custom_item_id, order_items_attributes: [:product_id, :price, :quantity])
end
答案 1 :(得分:0)
呈现表单的视图不在另一个控制器中,而不在orders
控制器中。视图的show
操作不包含order_items
的实例变量。这导致了错误消息。谢谢你的帮助。对于寻找嵌套表单资源的人来说,下面是一些有用的资源。