Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble nested model forms
    primarykey
    data
    text
    <p>I am still having problems with nested forms. Here is my form code:</p> <pre><code>&lt;%= form_for @account do |f| %&gt; &lt;%= f.label :account_type %&gt;&lt;br /&gt; &lt;%= f.text_field :account_type %&gt;&lt;br /&gt; &lt;%= f.fields_for :organizations do |builder| %&gt; &lt;%= builder.label :name %&gt;&lt;br /&gt; &lt;%= builder.text_field :name %&gt;&lt;br /&gt; &lt;%= builder.label :website %&gt;&lt;br /&gt; &lt;%= builder.text_field :website %&gt;&lt;br /&gt; &lt;%= f.fields_for :locations do |builder| %&gt; &lt;%= builder.label :phone %&gt;&lt;br /&gt; &lt;%= builder.text_field :phone %&gt;&lt;br /&gt; &lt;%= builder.label :toll_free_phone %&gt;&lt;br /&gt; &lt;%= builder.text_field :toll_free_phone %&gt;&lt;br /&gt; &lt;%= builder.label :fax %&gt;&lt;br /&gt; &lt;%= builder.text_field :fax %&gt;&lt;br /&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= f.submit "Add account" %&gt; &lt;% end %&gt; </code></pre> <p>The Account model:</p> <pre><code>class Account &lt; ActiveRecord::Base has_many :organizations accepts_nested_attributes_for :organizations end </code></pre> <p>The Organization model:</p> <pre><code>class Organization &lt; ActiveRecord::Base belongs_to :account has_many :locations accepts_nested_attributes_for :locations end </code></pre> <p>And the Location model:</p> <pre><code>class Location &lt; ActiveRecord::Base belongs_to :organization end </code></pre> <p>And lastly, the Accounts Controller:</p> <pre><code>class AccountsController &lt; ApplicationController def new @account = Account.new organization = @account.organizations.build organization.locations.build @header = "Create account" end def create @account = Account.new(params[:account]) if @account.save #handle success else render 'new' end end end </code></pre> <p>Here is the error I am getting:</p> <pre><code>ActiveRecord::UnknownAttributeError in AccountsController#create unknown attribute: locations Rails.root: C:/Documents and Settings/Corey Quillen/My Documents/rails_projects/shop_manager Application Trace | Framework Trace | Full Trace app/controllers/accounts_controller.rb:12:in `new' app/controllers/accounts_controller.rb:12:in `create' Request Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"xuZLSP+PSjdra3v51nIkJYZeXRM0X88iF135hPlp4sc=", "account"=&gt;{"account_type"=&gt;"Company", "organizations_attributes"=&gt;{"0"=&gt;{"name"=&gt;"Atlas", "website"=&gt;"www.atlas.com"}}, "locations"=&gt;{"phone"=&gt;"555-555-5555", "toll_free_phone"=&gt;"800-555-5555", "fax"=&gt;"512-555-5555"}}, "commit"=&gt;"Add account"} </code></pre> <p>Any help with this will be greatly appreciated. I have been looking at this now for a couple of hours.</p>
    singulars
    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