如何通过rails中的设计修复质量分配安全性错误?

时间:2021-09-05 22:45:43

I am using Devise in my Rails app for authentication. When I attempt to sign up as a user, I get an error saying: Can't mass-assign protected attributes: email, password, password_confirmation. I didn't receive this error until I added a new column to my Users table named 'owner'.

我在我的Rails应用程序中使用Devise进行身份验证。当我尝试以用户身份注册时,我收到错误消息:无法批量分配受保护的属性:email,password,password_confirmation。在我向名为“owner”的Users表中添加新列之前,我没有收到此错误。

User.rb

User.rb

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable   
    attr_accessible :owner, :email, :password, :password_confirmation, :admin, :as => :admin    
end

Migration to add user:

迁移添加用户:

class AddOwnerToUsers < ActiveRecord::Migration
  def change
    add_column :users, :owner, :boolean, :default => false
  end
end

Before I had added :owner, I dont' remember having this issue. Any advice on how to fix this?

在我添加之前:所有者,我不记得有这个问题。关于如何解决这个问题的任何建议?

1 个解决方案

#1


0  

I'm not that sure about the usages of :as in the attr_accessible. I've never used that. So far I've understood, you have to specify this when you updating mass attributes like:

对于attr_accessible的用法我不太确定。我从来没用过那个。到目前为止,我已经明白了,你必须在更新质量属性时指定这个:

User.new(params[:user], :as => :admin)

or,

要么,

@user = User.find(1)
@user.update_attributes(params[:user], :as => :admin)

#1


0  

I'm not that sure about the usages of :as in the attr_accessible. I've never used that. So far I've understood, you have to specify this when you updating mass attributes like:

对于attr_accessible的用法我不太确定。我从来没用过那个。到目前为止,我已经明白了,你必须在更新质量属性时指定这个:

User.new(params[:user], :as => :admin)

or,

要么,

@user = User.find(1)
@user.update_attributes(params[:user], :as => :admin)