0
votes

ActiveModel::ForbiddenAttributesError Extracted source (around line #3):

  1. def create
  2. @post = Post.find(params[:post_id])
  3. @comment = @post.comments.create!(params[:comment])
  4. redirect_to @post
  5. end
  6. end

Rails.root: C:/Users/ManU/Desktop/quick_blog Application Trace | Framework Trace | Full Trace

app/controllers/comments_controller.rb:4:in `create'

What i'm supposed to do to deal with this error..... please give me soln with path as well...i don't have prior knowlwdge to this.....

2
Using Windows to write rails applications? :) If you are new, I would go get virtualbox and install ubuntu and write ruby there. You get some nice features like FileUtils.rm, etc. if you are doing file manipulation ever.Timbinous

2 Answers

8
votes

you appear to be following a pre rails 4.0 tutorial with rails 4. You need to use strong params now.

http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

there is also a railscast on it that should help.

@comment = @post.comments.create!(params.require(:comment).permit!) 


@comment = @post.comments.create!(params.require(:comment).permit(:comment_text,:link))

The first will permit all params to be allowed, the latter will only allow comment_text and link to be accepted.

1
votes

If the system is throwing ActiveModel::ForbiddenAttributesError, that means you must be using strong_parameters gem or your rails should have version greater than 4, in which case strong_parameters gem is already included in that version. In that case, you add the following code on your application_controller.rb to get rid off from this error.

before_filter do
  resource = controller_name.singularize.to_sym
  method = "#{resource}_params"
  params[resource] &&= send(method) if respond_to?(method, true)
end