1

以下是我的 user.rb:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :password_expirable, :confirmable, :lockable, :timeoutable,
         :omniauthable

  valid_phone_regex = /(\d{3}|\(\d{3}\))(.|)\d{3}(.|)\d{4}/x
  no_spaces_regex = /\S/

  validates :first, presence: true
  validates :last, presence: true
  validates :organization, presence: true
  validates :work_phone, format: {with: valid_phone_regex}
  validates :mobile_phone, format: {with: valid_phone_regex}
  validates :fax_phone, format: {with: valid_phone_regex}
  validates :other_phone, format: {with: valid_phone_regex}
  validates :email, presence: true, uniqueness: {case_sensitive: false}
  validates :url, format: {with: no_spaces_regex}, presence: true
  before_save { |user| user.email = email.downcase }

  attr_accessible :first
  attr_accessible :last
  attr_accessible :salutation
  attr_accessible :organization
  attr_accessible :work_phone
  attr_accessible :mobile_phone
  attr_accessible :fax_phone
  attr_accessible :other_phone
  attr_accessible :address
  attr_accessible :city
  attr_accessible :state
  attr_accessible :zip
  attr_accessible :email
  attr_accessible :encrypted_password
  attr_accessible :reset_password_token
  attr_accessible :reset_password_sent_at
  attr_accessible :remember_created_at
  attr_accessible :sign_in_count
  attr_accessible :current_sign_in_at
  attr_accessible :last_sign_in_at
  attr_accessible :current_sign_in_ip
  attr_accessible :last_sign_in_ip
  attr_accessible :confirmation_token
  attr_accessible :confirmed_at
  attr_accessible :confirmation_sent_at
  attr_accessible :unconfirmed_email
  attr_accessible :failed_attempts
  attr_accessible :unlock_token
  attr_accessible :locked_at
end

我现在想要生成权威人士特定的东西,当我尝试这样做时,我收到以下错误消息:

C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing': undefined method `attr_accessible' for User (call 'User.connection' to establish a connection):Class (NoMethodError)
from C:/projects/rails/test/app/models/user.rb:21:in `<class:User>'

通常,当我不使用 ActiveRecord 作为基础时会发生这种情况,但这里不是这种情况。有任何想法吗?

4

2 回答 2

4

假设您使用的是 rails 4,attr_accessible则不再是一回事。

强参数现在是要走的路。如果您仍需要使用 attr_accessible,您可以安装Protected Attributes

于 2014-05-05T18:06:34.523 回答
0

您的 Rails 应用程序的版本是什么?

使用 rails 4,您必须使用受保护的属性才能使用attr_accessible. 推荐的方法是使用强参数

此外,您可以attr_accessible像这样使用

attr_accessible :first, :last, :salutation, :organization, :work_phone, :mobile_phone, :fax_phone, :other_phone, :address, :city, :state, :zip, :email, :encrypted_password, :reset_password_token, :reset_password_sent_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :confirmation_token, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at
于 2014-05-05T18:07:52.303 回答