I have a PurchaseOrder model and an Item model joined in a Quantity model via has_many through.
purchase_order.rb
class PurchaseOrder < ActiveRecord::Base
has_many :quantities, dependent: :destroy
has_many :items, :through => :quantities
accepts_nested_attributes_for :quantities, :reject_if => :all_blank, :allow_destroy => true
item.rb
class Item < ActiveRecord::Base
has_many :quantities, dependent: :destroy
has_many :purchase_orders, through: :quantities
accepts_nested_attributes_for :item, :reject_if => :all_blank
quantity.rb
class Quantity < ActiveRecord::Base
belongs_to :purchase_order
belongs_to :item
The Quantity join model has an extra attribute called 'amount':
Quantity id: nil, amount: nil, purchase_order_id: nil, item_id: nil
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 Unpermitted parameters: quantity
Here is the code in my new PurchaseOrder form:
<%= f.fields_for @q do |quantity| %>
<%= quantity.number_field :amount %><br>
<% end %>
I'm using rails 4, so in my PurchaseOrders controller, in the permit
function I have:
def purchase_order_params
params.require(:purchase_order).permit(:Date, :purchase_order_number,
:description, :quantity => [], :quantities_attributes => [], :amount,
:item_ids => [], :item_attributes => [], :supplier_ids => [])
end
This is the params hash that gets submitted in the form but still gives the error:
{"utf8"=>"✓", "authenticity_token"=>"DMZ6lROwRr11S3XLc2eXcb2In+G4weMZCJwhF0Bt8kQ=",
"purchase_order"=>{"Date(1i)"=>"2013", "Date(2i)"=>"12", "Date(3i)"=>"29",
"item_ids"=>["", "9"], "quantity"=>{"amount"=>"98"}, "supplier_ids"=>["", "4"],
"description"=>"", "amount"=>""}, "id"=>"170"}
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?
UPDATE Here is the full PurchaseOrder controller code: https://github.com/andrewcockerham/inventory/blob/master/app/controllers/purchase_orders_controller.rb
And here's the full sever error: (with quantity => []
)
Processing by PurchaseOrdersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DMZ6lROwRr11S3XLc2eXcb2In+G4weMZCJwhF0Bt8kQ=", "purchase_order"=> {"Date(1i)"=>"2014", "Date(2i)"=>"1", "Date(3i)"=>"1", "item_ids"=>["", "9"], "quantity"=>{"amount"=>"2345"}, "supplier_ids"=>["", "5"], "description"=>"2345", "amount"=>"2345"}, "id"=>"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", #<BigDecimal:7ff184e1e4c0,'0.2345E4',9(18)>], ["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"=>"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)
And with quantity => [:amount]
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'
The entire code can be viewed in the github repo.