随着最近对Rails 4的升级,使用类似以下代码来更新属性将不起作用,并会出现ActiveModel::ForbiddenAttributes
错误:
由于新版Rails默认启用了Strong Parameters,需要在控制器中指定参数名才能接受参数。要解决这个问题,需要在控制器中添加参数白名单,例如:
def article_params
params.require(:article).permit(:title, :content)
end
然后,可以使用更新属性的代码:
@article.update(article_params)
@user.update_attributes(params[:user], :as => :admin)
如果用户在模型中具有以下attr_accessible行:
attr_accessible :role_ids, :as =>admin
# or any attribute other than :role_ids contained within :user
在Rails 4中,您如何执行相同的任务?