0

ActiveModel::ForbiddenAttributesError 提取源(在第 3 行附近):

  1. 定义创建
  2. @post = Post.find(params[:post_id])
  3. @comment = @post.comments.create!(params[:comment])
  4. 重定向到@post
  5. 结尾
  6. 结尾

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

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

我应该做些什么来处理这个错误.....请给我路径以及解决方案......我对此没有先验知识......

4

2 回答 2

8

您似乎正在使用 rails 4 遵循 pre rails 4.0 教程。您现在需要使用强大的参数。

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))

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

于 2013-11-08T14:18:06.913 回答
1

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
于 2014-09-11T12:37:12.897 回答