Ruby on rails RubyonRails 2.3中的嵌套属性
根据这一点,我尝试使用嵌套表单 我有以下型号:Ruby on rails RubyonRails 2.3中的嵌套属性,ruby-on-rails,ruby,Ruby On Rails,Ruby,根据这一点,我尝试使用嵌套表单 我有以下型号: class User < ActiveRecord::Base has_one :customer accepts_nested_attributes_for :customer_attributes attr_accessible :customer_attributes, :email, :login, :password, :password_confirmation class Custom
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
我做错了什么
编辑:我变了
接受客户属性的嵌套属性
到
现在我得到了:
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
编辑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 -%>
改变
accepts_nested_attributes_for :customer_attributes
到
您必须为要处理的关系设置accepts_nested_attributes_。有关更多示例,请参见
class User < ActiveRecord::Base
has_one :customer
accepts_nested_attributes_for :customer
attr_accessible :customer, :email, :login,
:password, :password_confirmation
有一个:顾客
接受客户的\u嵌套\u属性\u
属性可访问:客户,:电子邮件,:登录,
:密码,:密码\u确认
希望这有帮助在第一封邮件中添加了一些详细信息您确定是Rails 2.3吗?看起来不是这样。。。显示其他详细信息。。。就像您正在使用的表单和控制器一样,Ruby Enterprise Edition的版本是2.3.8
<%= 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
accepts_nested_attributes_for :customer_attributes
accepts_nested_attributes_for :customer
class User < ActiveRecord::Base
has_one :customer
accepts_nested_attributes_for :customer
attr_accessible :customer, :email, :login,
:password, :password_confirmation