Ruby on Rails未经许可的参数有很多通过

时间:2013-09-30 12:38:38

标签: ruby-on-rails ruby

我有订单和产品,以及一个名为orders_products的连接表,该订单通过order_products有很多产品,并接受嵌套的产品。

当我尝试保存它时,一直说不允许的参数:order_product

PARAMS

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 %>

Params hash - {“utf8”=&gt;“√”,“authenticity_token”=&gt;“yBrH91u0OHTSPnCFO / 484Ff6CRtyRLSg5AKD1Lc33k4 =”,“order”=&gt; {“order_number”=&gt; “0121”,“order_product”=&gt; {“product”=&gt;“4”,“quantity”=&gt;“5”},“customer_id”=&gt;“3”},“commit”=&gt;“创建订单“}

未经许可的参数:order_product

1 个答案:

答案 0 :(得分:1)

你在这里缺少s

<%= f.fields_for :order_products do |fa| %>
相关问题