我是 RoR 的新手,我想我可以请你帮忙。我没有找到我正在寻找的具体答案。
我想使用 Devise 进行建模时遇到问题。Devise 设置了一个 Member 模型,我想要一个 SuperMember 模型,它比 Member 具有更多的属性和一些不同的视图。
我想设置一个嵌套表单来创建一个超级会员,同时在后台自动创建会员帐户。
Member.rb(由设计生成)
class Member < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end
超级会员.rb
class Supermember < ActiveRecord::Base
attr_accessible :first_name, :last_name
belongs_to :member, :dependent => :destroy
accepts_nested_attributes_for :member
end
超级成员.controllers.rb
def new
@supermember = Supermember.new
@supermember.member = Supermember.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @supermember }
end
end
def create
@supermember = Supermember.new(params[:supermember])
respond_to do |format|
if @supermember.save
format.html { redirect_to @supermember, notice: 'Supermember was successfully created.' }
format.json { render json: @supermember, status: :created, location: @supermember }
else
format.html { render action: "new" }
format.json { render json: @supermember.errors, status: :unprocessable_entity }
end
end
我尝试创建一个嵌套表单以生成成员和超级成员:
<%= simple_form_for(@supermember) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :first_name %>
<%= f.input :last_name %>
</div>
<% # Devise member %>
<%= f.fields_for :member do |m| %>
<div class="form-inputs">
<%= m.input :email, :required => true, :autofocus => true %>
<%= m.input :password, :required => true %>
<%= m.input :password_confirmation, :required => true %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
问题是,当我提交此表单时,我收到以下错误消息:
Can't mass-assign protected attributes: member_attributes
Application Trace | Framework Trace | Full Trace
app/controllers/supermembers.rb:44:in `new'
app/controllers/supermembers.rb:44:in `create'
我真的不明白如何解决它。你能帮我解决这个问题吗?非常感谢!