请使用新建议的params保护模型(strong_parameters)或将`protected_attributes`添加到gemfile中

当我在我的Relationship模型中添加attr_accessible时就发生了这种情况。

class Relationship < ActiveRecord::Base attr_accessible :followed_id end 

不使用Devise或protected_attributes gem,这有什么办法? 我知道在控制器中你调用一个需要和允许字段的私有方法。 这也是你应该在模型中做的事吗? 这里的约定是什么?

谢谢!

在Rails 4中,您使用强参数而不是受保护的属性。 (您不需要在gem文件中包含gem,因为它已经包含在内。)

您将Rails 3 attr_accessible代码从模型中取出并将相应的代码放入控制器中。 有关更多文档,请参见此处: https : //github.com/rails/strong_parameters

在你的情况下,像:

 class RelationshipController < ActionController::Base def create @relationship = Relationship.new(relationship_params) if @relationship.save # do something else # do something end end private def relationship_params params.require(:relationship).permit(:followed_id) end end 

编辑:

这是我刚刚发现的一篇很好的文章: http : //blog.sensible.io/2013/08/17/strong-parameters-by-example.html