我目前有一个表格,用于当用户提交带有评论的用户时:
<%= form_form @user =>
<%= f.fields_for :comments do |comment_form| %>
<%= comment_form.text_area :comment%>
<% end %>
我的用户控制器:
def new
@user = User.build
@user = User.comments.build
end
def create
@user = User.new(params[:user])
end
我的评论控制器:
def new
@comment = Comment.build
end
def create
@comment = Comment.new(params[:comment])
end
当我提交表单时,我收到此批量分配错误:
Can't mass-assign protected attributes: comments_attributes
app/controllers/users_controller.rb:15:in `new'
app/controllers/users_controller.rb:15:in `create'
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"BLoN2Ll0u98ijHB2Mgw7rhvQxeJVow6TqQWzpj94nNE=",
"user"=>{"name"=>"235235",
"city"=>"325235",
"province"=>"235235",
"comments_attributes"=>{"0"=>{"comment"=>"235235235"},
"1"=>{"terms"=>"1"}}},
"commit"=>"Submit"}
User
班级:
attr_accessible :name, :city, :province
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments
Comment
班级:
attr_accessible :comment, :terms
belongs_to :user