qutron
qutron

Reputation: 1760

nested attributes in ruby on rails 2.3

According to this answer I try to work with nested form.

I have following models:

class User < ActiveRecord::Base
has_one :customer
accepts_nested_attributes_for :customer_attributes
attr_accessible  :customer_attributes, :email, :login, 
                 :password, :password_confirmation 


class Customer < ActiveRecord::Base
  belongs_to :user
end

I try to update customer and user via nested form< but I get this error message:

No association found for name `customers_attributes'. Has it been defined yet?

What am I doing wrong?

EDIT: I changed

accepts_nested_attributes_for :customer_attributes

to

accepts_nested_attributes_for :customer.

Now I got:

Can't mass-assign these protected attributes: customer

and INSERT only in users table

EDIT #2: Now user class looks like this:

class User < ActiveRecord::Base
has_one :customer
accepts_nested_attributes_for :customer
attr_accessible  :customer, :email, :login, 
                 :password, :password_confirmation 

I got following error:

ActiveRecord::AssociationTypeMismatch in AccountController#signup
Customer(#22143760) expected, got HashWithIndifferentAccess(#17668940)

gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:259:in `raise_on_type_mismatch'
gems/activerecord-2.3.8/lib/active_record/associations/has_one_association.rb:55:in `replace'
gems/activerecord-2.3.8/lib/active_record/associations.rb:1287:in `customer='
gems/ree-1.8.7-2011.03@rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `send'
gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `assign_attributes'
gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in `attributes='
gems/activerecord-2.3.8/lib/active_record/base.rb:2473:in `initialize'
app/controllers/account_controller.rb:31:in `new'
app/controllers/account_controller.rb:31:in `signup'

EDIT #3: Just in case. Here is my singup form:

<%= error_messages_for :user %>
<% form_for :user do |f| -%>
<p><label for="login">Login</label><br/>
<%= f.text_field :login %></p>

<p><label for="email">Email</label><br/>
<%= f.text_field :email %></p>

<p><label for="password">Password</label><br/>
<%= f.password_field :password %></p>

<p><label for="password_confirmation">Confirm Password</label><br/>
<%= f.password_field :password_confirmation %></p>

<% f.fields_for :customer do |c| %>

  <p><%= c.label :first_name %><br>
  <%= c.text_field :first_name %></p>
  <p><%= c.label :last_name %><br>
  <%= c.text_field :last_name %></p>
  <p><%= c.label :phone %><br>
  <%= c.text_field :phone %></p>
  <p><%= c.label :date_of_birth %><br>
  <%= c.text_field :date_of_birth %></p>
  <p><%= c.label :avatar %><br>
  <%= c.text_field :avatar %></p>
 <% end %>

<p><%= submit_tag 'Sign up' %></p>
<% end -%>

Controller method:

  def signup
    @user = User.new(params[:user])
    return unless request.post?
    @user.save!
    self.current_user = @user
    redirect_back_or_default(:controller => '/account', :action => 'index')
    flash[:notice] = "Thanks for signing up!"
  rescue ActiveRecord::RecordInvalid
    render :action => 'signup'
  end

Upvotes: 0

Views: 1127

Answers (2)

nathanvda
nathanvda

Reputation: 50057

class User < ActiveRecord::Base
  has_one :customer
  accepts_nested_attributes_for :customer
  attr_accessible  :customer, :email, :login, 
             :password, :password_confirmation

Hope this helps

Upvotes: 1

lucapette
lucapette

Reputation: 20724

Change

accepts_nested_attributes_for :customer_attributes

to

accepts_nested_attributes_for :customer

You have to set the accepts_nested_attributes_for on the relation you what to work with. See docs for further examples

Upvotes: 0

Related Questions