首页 > 解决方案 > rails 5 中 attr_protected 的替代品是什么?

问题描述

class Oregano < ApplicationRecord
  attr_protected :person_id
end

这让我返回一个错误

/activerecord-5.0.5/lib/active_record/dynamic_matchers.rb:21:in `method_missing': undefined method `attr_protected' for #<Class:0x000055b38448bd58> (NoMethodError). 

Rails 5升级中的替代品应该是什么?

标签: ruby-on-rails-5

解决方案


Rails 5 不允许质量分配

不要在模型中使用 attr_protected ,而是:person_id使用强参数。您将在 OreganosController 中执行此操作:

def oregano_params
  params.require(:oregano).permit(:person_id)
end

推荐阅读