Note that there are some explanatory texts on larger screens.

plurals
  1. PONested form input for join table attributes in a has_many through association, parameter is unpermitted
    primarykey
    data
    text
    <p>I have a PurchaseOrder model and an Item model joined in a Quantity model via has_many through.</p> <p>purchase_order.rb</p> <pre><code>class PurchaseOrder &lt; ActiveRecord::Base has_many :quantities, dependent: :destroy has_many :items, :through =&gt; :quantities accepts_nested_attributes_for :quantities, :reject_if =&gt; :all_blank, :allow_destroy =&gt; true </code></pre> <p><em>item.rb</em></p> <pre><code>class Item &lt; ActiveRecord::Base has_many :quantities, dependent: :destroy has_many :purchase_orders, through: :quantities accepts_nested_attributes_for :item, :reject_if =&gt; :all_blank </code></pre> <p><em>quantity.rb</em></p> <pre><code>class Quantity &lt; ActiveRecord::Base belongs_to :purchase_order belongs_to :item </code></pre> <p>The Quantity join model has an extra attribute called 'amount':</p> <pre><code>Quantity id: nil, amount: nil, purchase_order_id: nil, item_id: nil </code></pre> <p>In the new PurchaseOrder form, I want to set the amount in the Quantity join table for each new item that is created. I believe I have it almost working, but I get an error when saving the new PurchaseOrder that says <code>Unpermitted parameters: quantity</code></p> <p>Here is the code in my new PurchaseOrder form:</p> <pre><code>&lt;%= f.fields_for @q do |quantity| %&gt; &lt;%= quantity.number_field :amount %&gt;&lt;br&gt; &lt;% end %&gt; </code></pre> <p>I'm using rails 4, so in my PurchaseOrders controller, in the <code>permit</code> function I have:</p> <pre><code>def purchase_order_params params.require(:purchase_order).permit(:Date, :purchase_order_number, :description, :quantity =&gt; [], :quantities_attributes =&gt; [], :amount, :item_ids =&gt; [], :item_attributes =&gt; [], :supplier_ids =&gt; []) end </code></pre> <p>This is the params hash that gets submitted in the form but still gives the error:</p> <pre><code>{"utf8"=&gt;"✓", "authenticity_token"=&gt;"DMZ6lROwRr11S3XLc2eXcb2In+G4weMZCJwhF0Bt8kQ=", "purchase_order"=&gt;{"Date(1i)"=&gt;"2013", "Date(2i)"=&gt;"12", "Date(3i)"=&gt;"29", "item_ids"=&gt;["", "9"], "quantity"=&gt;{"amount"=&gt;"98"}, "supplier_ids"=&gt;["", "4"], "description"=&gt;"", "amount"=&gt;""}, "id"=&gt;"170"} </code></pre> <p>Why is the quantity parameter still unpermitted? Why will it not save the :amount in the Quantity join table when I submit the form? Or do I have this setup all wrong?</p> <p><strong>UPDATE</strong> Here is the full PurchaseOrder controller code: <a href="https://github.com/andrewcockerham/inventory/blob/master/app/controllers/purchase_orders_controller.rb" rel="nofollow">https://github.com/andrewcockerham/inventory/blob/master/app/controllers/purchase_orders_controller.rb</a></p> <p>And here's the full sever error: (with <code>quantity =&gt; []</code>)</p> <pre><code>Processing by PurchaseOrdersController#update as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"DMZ6lROwRr11S3XLc2eXcb2In+G4weMZCJwhF0Bt8kQ=", "purchase_order"=&gt; {"Date(1i)"=&gt;"2014", "Date(2i)"=&gt;"1", "Date(3i)"=&gt;"1", "item_ids"=&gt;["", "9"], "quantity"=&gt;{"amount"=&gt;"2345"}, "supplier_ids"=&gt;["", "5"], "description"=&gt;"2345", "amount"=&gt;"2345"}, "id"=&gt;"197"} PurchaseOrder Load (0.2ms) SELECT "purchase_orders".* FROM "purchase_orders" WHERE "purchase_orders"."id" = ? LIMIT 1 [["id", "197"]] Unpermitted parameters: quantity (0.1ms) begin transaction Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = ? LIMIT 1 [["id", 9]] Item Load (0.1ms) SELECT "items".* FROM "items" INNER JOIN "quantities" ON "items"."id" = "quantities"."item_id" WHERE "quantities"."purchase_order_id" = ? [["purchase_order_id", 197]] SQL (0.7ms) INSERT INTO "quantities" ("created_at", "item_id", "purchase_order_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Wed, 01 Jan 2014 21:00:40 UTC +00:00], ["item_id", 9], ["purchase_order_id", 197], ["updated_at", Wed, 01 Jan 2014 21:00:40 UTC +00:00]] Supplier Load (0.1ms) SELECT "suppliers".* FROM "suppliers" WHERE "suppliers"."id" = ? LIMIT 1 [["id", 5]] Supplier Load (0.1ms) SELECT "suppliers".* FROM "suppliers" INNER JOIN "orders" ON "suppliers"."id" = "orders"."supplier_id" WHERE "orders"."purchase_order_id" = ? [["purchase_order_id", 197]] SQL (0.3ms) INSERT INTO "orders" ("created_at", "purchase_order_id", "supplier_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Wed, 01 Jan 2014 21:00:40 UTC +00:00], ["purchase_order_id", 197], ["supplier_id", 5], ["updated_at", Wed, 01 Jan 2014 21:00:40 UTC +00:00]] PurchaseOrder Exists (0.1ms) SELECT 1 AS one FROM "purchase_orders" WHERE ("purchase_orders"."purchase_order_number" = '20140101-04' AND "purchase_orders"."id" != 197) LIMIT 1 SQL (0.2ms) UPDATE "purchase_orders" SET "description" = ?, "amount" = ?, "Date" = ?, "updated_at" = ? WHERE "purchase_orders"."id" = 197 [["description", "2345"], ["amount", #&lt;BigDecimal:7ff184e1e4c0,'0.2345E4',9(18)&gt;], ["Date", Wed, 01 Jan 2014], ["updated_at", Wed, 01 Jan 2014 21:00:40 UTC +00:00]] (1.1ms) commit transaction `Redirected to http://localhost:3001/purchase_orders/197` Completed 302 Found in 28ms (ActiveRecord: 3.6ms) Started GET "/purchase_orders/197" for 127.0.0.1 at 2014-01-01 15:00:40 -0600 Processing by PurchaseOrdersController#show as HTML Parameters: {"id"=&gt;"197"} PurchaseOrder Load (0.1ms) SELECT "purchase_orders".* FROM "purchase_orders" WHERE "purchase_orders"."id" = ? LIMIT 1 [["id", "197"]] DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.where(...).all instead. (called from show at /Users/Andrew/code/RailsCode/TVA2/TVAInventory/app/controllers/purchase_orders_controller.rb:23) Quantity Load (0.2ms) SELECT "quantities".* FROM "quantities" WHERE "quantities"."purchase_order_id" = 197 Supplier Load (0.1ms) SELECT "suppliers".* FROM "suppliers" INNER JOIN "orders" ON "suppliers"."id" = "orders"."supplier_id" WHERE "orders"."purchase_order_id" = ? [["purchase_order_id", 197]] Item Load (0.1ms) SELECT "items".* FROM "items" INNER JOIN "quantities" ON "items"."id" = "quantities"."item_id" WHERE "quantities"."purchase_order_id" = ? [["purchase_order_id", 197]] Rendered purchase_orders/show.html.erb within layouts/application (3.5ms) Rendered layouts/_header.html.erb (0.6ms) Completed 200 OK in 16ms (Views: 12.7ms | ActiveRecord: 0.5ms) </code></pre> <p>And with <code>quantity =&gt; [:amount]</code></p> <pre><code>ActiveRecord::UnknownAttributeError (unknown attribute: quantity): app/controllers/purchase_orders_controller.rb:79:in `block in update' app/controllers/purchase_orders_controller.rb:78:in `update' </code></pre> <p>The entire code can be viewed in the github repo. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload