我有订单和产品,还有一个名为 orders_products 的连接表,订单通过 order_products 有很多产品,并接受嵌套属性。
当我尝试保存时,它一直说未经许可的参数:order_product
参数
def order_params
params.require(:order).permit(:id, :order_number, :customer_id, {order_products_attributes: [:id, :order, :product, :quantity ]}, {:product_ids => []})
end
订购型号
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_products, class_name: "OrderProduct"
has_many :products, through: :order_products
accepts_nested_attributes_for :order_products, :allow_destroy => true
end
订购产品型号
class OrderProduct < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
订单控制器新动作
def new
@order = Order.new
@order.order_products.build
end
订单
<%= simple_form_for @order do |f| %>
<%= f.input :order_number %>
<%= f.fields_for :order_product do |fa| %>
<%= fa.input :product, collection: Product.all %>
<%= fa.input :quantity %>
<% end %>
<%= f.association :customer, as: :select %>
<%= f.submit %>
<% end %>
参数哈希 - {"utf8"=>"√","authenticity_token"=>"yBrH91u0OHTSPnCFO/484Ff6CRtyRLSg5AKD1Lc33k4=", "order"=>{"order_number"=>"0121", "order_product"=>{"product"= >"4", "quantity"=>"5"}, "customer_id"=>"3"}, "commit"=>"创建订单"}
不允许的参数:order_product