ActiveModel::ForbiddenAttributesError when creating new user

后端 未结 7 1821
礼貌的吻别
礼貌的吻别 2020-11-22 15:59

I have this model in Ruby but it throws a ActiveModel::ForbiddenAttributesError

class User < ActiveRecord::Base
  attr_accessor :password
  v         


        
7条回答
  •  耶瑟儿~
    2020-11-22 16:31

    There is an easier way to avoid the Strong Parameters at all, you just need to convert the parameters to a regular hash, as:

    unlocked_params = ActiveSupport::HashWithIndifferentAccess.new(params)
    
    model.create!(unlocked_params)
    

    This defeats the purpose of strong parameters of course, but if you are in a situation like mine (I'm doing my own management of allowed params in another part of my system) this will get the job done.

提交回复
热议问题