CommentsController中的ActiveModel :: ForbiddenAttributesError #create

use*_*901 0 ruby ruby-on-rails ruby-on-rails-4

ActiveModel :: ForbiddenAttributesError提取的源(第3行):

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

Rails.root:C:/ Users/ManU/Desktop/quick_blog应用程序跟踪| 框架跟踪| 完整追踪

app/controllers/comments_controller.rb:4:在'create'中

我应该怎么做来处理这个错误.....请给我解决路径以及...我没有事先知道这个.....

Doo*_*oon 8

您似乎正在使用rails 4跟踪pre rails 4.0教程.您现在需要使用强params.

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

还有一个应该有帮助的railscast.

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


@comment = @post.comments.create!(params.require(:comment).permit(:comment_text,:link))
Run Code Online (Sandbox Code Playgroud)

第一个允许允许所有参数,后者只允许comment_text并被link接受.