1

我有一个 Rails 应用程序,它使用许可和 cancan gem 进行身份验证和授权。最初,应用程序使用 User 模型作为 Clearance 使用的 user_model。现在,由于一些设计更改,我们希望有另一个模型类 Person 被 Clearance 用作 user_model。Person 模型已经就位,并且在应用程序上下文中具有特定于 Person 模型的列。

Rails 版本:3.2.14
Ruby 版本:1.9.3-p487
清除版本:0.16.3

为此,我们进行了以下更改:

  1. Clearance 所需的字段(email、encrypted_pa​​ssword、remember_token、salt、confirmation_token)从 User 模型迁移到 Person 模型。
  2. 使 Person 模型包括 Clearance::User
  3. 按照http://rubydoc.info/github/thoughtbot/clearance/frames中的建议,将 Clearance 初始化程序更改为设置 *user_model = Person*

    Clearance.configure do |config|
      config.user_model = Person
    end
    
  4. 更新了 Person 模型的工厂定义,如下所示:

    FactoryGirl.define do
      sequence(:email) {|n| "user#{n}@example.com" }
      factory :person do
        ignore do
          group nil
        end
        email
        password { "password" }
        company
        first_name { FactoryGirl.generate(:name).capitalize }
        last_name  "Test"
        groups { [group || FactoryGirl.create(:group)] }
      end
    end
    

在这些更改之后,当我运行与人员相关的规范时,它无法使用工厂创建人员对象。我得到以下问题。

    1) Person 
       Failure/Error: it { should validate_presence_of(:first_name) }
       NameError: undefined local variable or method `encrypted_password' for #<Person:0x00000001e7bcc8>
       # ./spec/models/person_spec.rb:8:in `block (2 levels) in <top (required)>'

我参考了文档,关于清除组的谷歌组主题,论坛帖子,但无法解决问题。有没有人做过类似的?你能帮我解决这个问题吗?

4

1 回答 1

1

看起来您的 Person 类没有 encrypted_pa​​ssword 列。确保它有这个,还有下面的字段

t.string :email
t.string :encrypted_password, :limit => 128
t.string :confirmation_token, :limit => 128
t.string :remember_token, :limit => 128

如果您发布您的 schema.rb 或 person.rb 文件,这可以得到确认。

于 2014-02-07T03:45:50.280 回答