2011-11-17 146 views
0

根據此answer我嘗試使用嵌套窗體。Ruby on Rails嵌套屬性2.3

我有以下型號:

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 

我試着通過嵌套形式<更新客戶和用戶,但我收到此錯誤信息:

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

我在做什麼錯?

編輯:我改變

accepts_nested_attributes_for :customer_attributes

accepts_nested_attributes_for :customer. 

現在,我得到:

Can't mass-assign these protected attributes: customer 

,只有在用戶表中插入

編輯#2: 現在用戶類看起來是這樣的:

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

我得到了以下錯誤:

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/[email protected]/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' 

編輯#3:以防萬一。這裏是我的singup形式:

<%= 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 -%> 

控制器的方法:

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 
+0

你確定這是Rails的2.3?它似乎不那麼...顯示其他細節...像你使用的窗體和控制器... – lucapette

+0

這是2.3.8與Ruby企業版 – qutron

回答

0

變化

accepts_nested_attributes_for :customer_attributes 

accepts_nested_attributes_for :customer 

你必須設置的關係,你什麼accepts_nested_attributes_for跟...共事。更多的例子見docs

+0

增加了一些細節的第一篇文章 – qutron

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

希望這有助於

+0

請參閱編輯#2 – qutron