ActiveModel::ForbiddenAttributesError when creating new user

后端 未结 7 1860
礼貌的吻别
礼貌的吻别 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:43

    If you are on Rails 4 and you get this error, it could happen if you are using enum on the model if you've defined with symbols like this:

    class User
      enum preferred_phone: [:home_phone, :mobile_phone, :work_phone]
    end
    

    The form will pass say a radio selector as a string param. That's what happened in my case. The simple fix is to change enum to strings instead of symbols

    enum preferred_phone: %w[home_phone mobile_phone work_phone]
    # or more verbose
    enum preferred_phone: ['home_phone', 'mobile_phone', 'work_phone']
    

提交回复
热议问题