当我向关系模型添加 attr_accessible 时,发生了这种情况。
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
end
如果不使用 Devise 或 protected_attributes gem,解决这个问题的方法是什么?我知道在控制器中您调用需要和允许字段的私有方法。这也是你应该在模型中做的事情吗?这里的约定是什么?
Thanks!
在 Rails 4 中,您使用强参数而不是受保护的属性。 (您不需要将 gem 包含在 gemfile 中,因为它已经包含在内。)
您从模型中取出 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
Edit:
这是我刚刚看到的一篇关于此的好文章:http://blog.sensible.io/2013/08/17/strong-parameters-by-example.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)