title: In Rails 5.1, how do I use one form to update two tables?
body: <p>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:</p>
  
  <p><a href="https://stackoverflow.com/questions/24746906/ruby-on-rails-saving-in-two-tables-from-one-form">Ruby on Rails Saving in two tables from one form</a></p>
  
  <p>I have mostly copied this answer but I still can't get it to work. Here are the relevant bits of code:</p>
  
  <p>Models - I have two models, supplier and account. Each supplier should have one account. </p>
  
  <pre><code>class Supplier &lt; ApplicationRecord
    has_one :account
    accepts_nested_attributes_for :account
  end
  
  class Account &lt; ApplicationRecord
    belongs_to :supplier
  end
  </code></pre>
  
  <p>Form - not sure if this is entirely correct</p>
  
  <pre><code>&lt;h2&gt;Please enter a supplier&lt;/h2&gt;
    &lt;%= form_for(@supplier) do |form| %&gt;
  
  &lt;p&gt;
    &lt;%= form.label :name %&gt;&lt;br&gt;
    &lt;%= form.text_field :name %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
   &lt;%= form.fields_for :account do |f| %&gt;
   &lt;%= f.label :account_number %&gt;&lt;br&gt;
   &lt;%= f.text_field :account_number %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
   &lt;%= form.submit %&gt;
  &lt;/p&gt;
   &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>And finally, the <code>SuppliersController</code></p>
  
  <pre><code>class SuppliersController &lt; 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
  </code></pre>
  
  <p>I am getting an undefined method error on the second line of the new action of the <code>SuppliersController</code> and I don't know why.</p>
  
  <p>Debugging printout for create action in <code>SupplierController</code>:</p>
  
  <p>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"}</p>
  
  <p>(0.1ms)  begin transaction</p>
  
  <p>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"]]</p>
  
  <p>(0.6ms)  commit transaction
     (0.0ms)  begin transaction</p>
  
  <p>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]]</p>
  
  <p>(0.6ms)  commit transaction</p>
  
  <p>Supplier: #Supplier id: 6, created_at: "2017-11-03 14:25:25", updated_at: "2017-11-03 14:25:25", name: "Bob"</p>
  
  <p>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</p>
  
  <p>(0.0ms)  begin transaction
     (0.0ms)  commit transaction</p>
  
  <p>Redirected to <a href="http://localhost:3000/suppliers" rel="nofollow noreferrer">http://localhost:3000/suppliers</a>
  Completed 302 Found in 51ms (ActiveRecord: 4.6ms)</p>