Derptacos
Derptacos

Reputation: 177

Rails form submission error

I currently have a form for when a user submits a user with a comment as such:

<%= form_form @user =>
<%= f.fields_for :comments do |comment_form| %>
    <%= comment_form.text_area :comment%>
<% end %>

My users controller:

def new 
 @user = User.build
 @user = User.comments.build
end

def create
 @user = User.new(params[:user])
end 

My comments controller:

def new
 @comment = Comment.build
end

def create 
 @comment = Comment.new(params[:comment])
end

When I submit the form I receive this mass assignment error:

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 class:

attr_accessible  :name, :city, :province
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments

Comment class:

attr_accessible :comment, :terms
belongs_to :user

Upvotes: 0

Views: 146

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51161

Add :comments_attributes to User's attr_accessible.

Upvotes: 3

Related Questions