为了账号安全,请及时绑定邮箱和手机立即绑定

ActiveModel:创建新用户时的ForbidenAttributesError

ActiveModel:创建新用户时的ForbidenAttributesError

阿波罗的战车 2019-07-17 09:31:47
ActiveModel:创建新用户时的ForbidenAttributesError我在Ruby中有这个模型,但是它抛出了一个ActiveModel::ForbiddenAttributesErrorclass User < ActiveRecord::Base   attr_accessor :password   validates :username, :presence => true, :uniqueness => true, :length => {:in => 3..20}   VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i   validates :email, presence: true, :uniqueness => true, format: { with: VALID_EMAIL_REGEX }   validates :password, :confirmation => true   validates_length_of :password, :in => 6..20, :on => :create   before_save :encrypt_password   after_save :clear_password  def encrypt_password    if password.present?       self.salt = BCrypt::Engine.generate_salt      self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)     end   end   def clear_password    self.password = nil   endend当我运行这个动作  def create    @user = User.new(params[:user])     if @user.save       flash[:notice] = "You Signed up successfully"       flash[:color]= "valid"     else       flash[:notice] = "Form is invalid"       flash[:color]= "invalid"     end     render "new"   end在……上面ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux].你能告诉我如何消除这个错误或建立一个适当的用户登记表吗?
查看完整描述

3 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

如果使用ActiveAdmin,请不要忘记在模型寄存器块中也有一个许可证_params:

ActiveAdmin.register Api::V1::Person do
  permit_params :name, :address, :etcend

这些需要与控制器中的设置一起设置:

def api_v1_person_params  params.require(:api_v1_person).permit(:name, :address, :etc)end

否则,您将得到错误:

ActiveModel::ForbiddenAttributesError


查看完整回答
反对 回复 2019-07-17
  • 3 回答
  • 0 关注
  • 556 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信