I have been trying to get one form to update two tables in Ruby on Rails. I have done a fairly exhaustive search on here and I'm finding the guides a little confusing. The closest answer I have found is this:
Ruby on Rails Saving in two tables from one form
I have mostly copied this answer but I still can't get it to work. Here are the relevant bits of code:
Models - I have two models, supplier and account. Each supplier should have one account.
class Supplier < ApplicationRecord
has_one :account
accepts_nested_attributes_for :account
end
class Account < ApplicationRecord
belongs_to :supplier
end
Form - not sure if this is entirely correct
<h2>Please enter a supplier</h2>
<%= form_for(@supplier) do |form| %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.fields_for :account do |f| %>
<%= f.label :account_number %><br>
<%= f.text_field :account_number %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<% end %>
And finally, the SuppliersController
class SuppliersController < ApplicationController
def index
@suppliers = Supplier.all
end
def new
@supplier = Supplier.new
@supplier = build_account
end
def create
@supplier = suppliers.build(supplier_params)
if @supplier.save
redirect_to suppliers_path
else
redirect_to root_path
end
end
private
def supplier_params
params.require(:supplier).permit(:name, account_attributes:
[:account_number])
end
end
I am getting an undefined method error on the second line of the new action of the SuppliersController
and I don't know why.
Debugging printout for create action in SupplierController
:
Started POST "/suppliers" for 127.0.0.1 at 2017-11-03 08:25:25 -0600 Processing by SuppliersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"KEqvPgjrYmuBux3qWCQJLAkSLQ4z1ns4HsK2P9sWlhVegpjhik/JoWT3vlL/kP42XpU8adllOaqSA16izYJ0SA==", "supplier"=>{"name"=>"", "account_attributes"=>{"account_number"=>""}}, "commit"=>"Create Supplier"}
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "suppliers" ("created_at", "updated_at", "name") VALUES (?, ?, ?) [["created_at", "2017-11-03 14:25:25.388933"], ["updated_at", "2017-11-03 14:25:25.388933"], ["name", "Bob"]]
(0.6ms) commit transaction (0.0ms) begin transaction
Supplier Load (1.0ms) SELECT "suppliers".* FROM "suppliers" WHERE "suppliers"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]] SQL (0.3ms) INSERT INTO "accounts" ("created_at", "updated_at", "account_number", "supplier_id") VALUES (?, ?, ?, ?) [["created_at", "2017-11-03 14:25:25.424367"], ["updated_at", "2017-11-03 14:25:25.424367"], ["account_number", "456456456"], ["supplier_id", 6]]
(0.6ms) commit transaction
Supplier: #Supplier id: 6, created_at: "2017-11-03 14:25:25", updated_at: "2017-11-03 14:25:25", name: "Bob"
Account: #Account id: 1, created_at: "2017-11-03 14:25:25", updated_at: "2017-11-03 14:25:25", account_number: "456456456", supplier_id: 6
(0.0ms) begin transaction (0.0ms) commit transaction
Redirected to http://localhost:3000/suppliers Completed 302 Found in 51ms (ActiveRecord: 4.6ms)
INFO -- : Parameters:
soon after the Started POST line. It is likely that you need to permit the parameters for the :account. – Phil@supplier
and then an undefined methodbuild_account
along with a "Completed 500 Internal Service Error" When you say log, you mean what gets printed in the in command line? (I've honestly never used this) – jive14