在轨道上的ruby中的嵌套属性2.3

时间:2011-11-17 14:26:27

标签: ruby-on-rails ruby

根据这个answer,我尝试使用嵌套表单。

我有以下模特:

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

我尝试通过嵌套表单&lt;更新客户和用户但我收到此错误消息:

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:编辑#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/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'

编辑#3:以防万一。这是我的单身形式:

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

2 个答案:

答案 0 :(得分:1)

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

希望这有帮助

答案 1 :(得分:0)

更改

accepts_nested_attributes_for :customer_attributes

accepts_nested_attributes_for :customer

您必须在您要使用的关系上设置accepts_nested_attributes_for。有关更多示例,请参阅docs