![](/img/trans.png)
[英]NoMethodError: undefined method `encrypted_password devise gem
[英]Why is the devise method encrypted_password= undefined?
我正在嘗試為我的管理模型設置設計。 admin模型繼承自Author。 我已完成文檔中建議的所有設計設置並運行所有遷移等。我的模型如下:
作者類:
class Author < ActiveRecord::Base
has_many :articles
validates :first_name, :surname, :email, presence: true
end
管理員類:
class Admin < Author
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
end
設計遷移:
class DeviseCreateAdmins < ActiveRecord::Migration
def change
create_table(:admins) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.inet :current_sign_in_ip
t.inet :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :admins, :email, unique: true
add_index :admins, :reset_password_token, unique: true
#add_index :admins, :confirmation_token, unique: true
#add_index :admins, :unlock_token, unique: true
end
模式顯示DB中的encrypted_password
。
create_table "admins", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
嘗試在我的某個測試或rails控制台中創建管理員時收到的錯誤如下:
NoMethodError:
undefined method `encrypted_password=' for #<Admin:0x007fc1c3269e18>
正在使用的測試是:
scenario 'Signing in with correct credentials' do
Admin.create(first_name: "Jonathan", surname: "Sayer", email: "test@email.com",
password: "password", password_confirmation: "password")
visit '/login'
fill_in 'Email', with: "test@email.com"
fill_in 'Password', with: "password"
click_button "Log in"
expect(page.current_path).to eq root_path
end
帶有錯誤消息:
1) Signing in Signing in with correct credentials
Failure/Error:
Admin.create(first_name: "Jonathan", surname: "Sayer", email: "test@email.com",
password: "password", password_confirmation: "password")
NoMethodError:
undefined method `encrypted_password=' for #<Admin:0x007fc1c3269e18>
# /Users/jonathansayer/.rvm/gems/ruby-2.3.0/gems/devise-3.5.6/lib/devise/models/database_authenticatable.rb:43:in `password='
# ./spec/features/logging_in_and_out_spec.rb:5:in `block (2 levels) in <top (required)>'
任何幫助都將非常感謝!! 謝謝
解
將self.table_name = "admins"
添加到您的Admin
模型以修復錯誤。
說明
我看到Admin
模型繼承自Author
模型。 除非您嘗試實現STI(單表繼承),否則不推薦使用哪個btw。 這里不是這種情況,因為你有兩個模型的表。 這就是實際問題。
默認情況下,您會看到ActiveRecord使用模型的復數名稱將Rails模型連接到數據庫中的相應表。
因此,對於Author
模型,在這種情況下,它對應於DB中的authors
表。 現在,由於Admin
是從Author
繼承的,因此它也連接到authors
表。
這就是為什么您期望在admins
表中出現的所有字段都不存在的原因。 因為現在Admin
模型只知道authors
表中的字段
修復方法是顯式指定模型的表名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.