我阅读了很多相关的帖子,但找不到为什么它对我不起作用。我仍然有一个“无法批量分配受保护的属性:配置文件”......我做错了什么?
我有一个用户模型和一个具有一对一关系的相关配置文件模型。这里是用户模型(简化)
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :profile_attributes, :profile_id
has_secure_password
has_one :profile
accepts_nested_attributes_for :profile
end
轮廓模型
class Profile < ActiveRecord::Base
attr_accessible :bio, :dob, :firstname, :gender, :lastname, :user_id
belongs_to :user
end
我的用户控制器
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
def create
@user = User.new(params[:user])
@user.build_profile
respond_to do |format|
if @user.save
format.html { redirect_to @user, flash: {success: 'User was successfully created. Welcome !'} }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
如果它可以提供任何帮助,那么 User 和 Profile 都已经搭建好了。
我也尝试在用户 attr_accessible 中使用 ':profiles_attributes' 而不是 'profile_attributes',同样的问题......
也尝试过,在用户控制器中使用“@user.profiles.build”而不是“@user.build_profile”......结果相同......
任何解释的帮助都会很棒(我是个菜鸟,所以请原谅我)
编辑我使用的简单形式
<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>
<%= f.simple_fields_for :profiles do |p| %>
<div class="nested-form-inputs">
<%= p.input :lastname %>
<%= p.input :firstname %>
</div>
<% end %>
<div class="form-inputs">
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
干杯