Rails表单提交错误

时间:2013-06-17 15:05:21

标签: ruby-on-rails ruby-on-rails-3 forms

我目前有一个表单,用于当用户向用户提交评论时:

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

1 个答案:

答案 0 :(得分:3)

:comments_attributes添加到User的{​​{1}}。