ruby on rails - Why is the devise method encrypted_password= undefined? -
i trying set devise admin model. admin model inherits author. i've done devise setup suggested in documentation , run migrations etc. models below:
author class:
class author < activerecord::base has_many :articles validates :first_name, :surname, :email, presence: true end admin class:
class admin < author devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable end devise migration:
class devisecreateadmins < activerecord::migration def change create_table(:admins) |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 # if using reconfirmable ## lockable # t.integer :failed_attempts, default: 0, null: false # if lock strategy :failed_attempts # t.string :unlock_token # if unlock strategy :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 schema showing encrypted_password in db.
create_table "admins", force: :cascade |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 the error receive when trying create admin in 1 of tests, or in rails console, follows:
nomethoderror: undefined method `encrypted_password=' #<admin:0x007fc1c3269e18> the test being used is:
scenario 'signing in correct credentials' 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 with error message:
1) signing in signing in 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=' #<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)>' any appreciated!! thanks
solution
add self.table_name = "admins" admin model fix error.
explanation
i see admin model inherited author model. btw not recommended unless trying implement sti (single table inheritance). not case here have tables both models. , here lies actual problem.
you see default activerecord connects rails model it's corresponding table in db using pluralized name of model.
so in case author model corresponds authors table in db. since admin inherited author connected authors table.
and why fields expect present in admins table not present. because right admin model knows fields in authors table
the fix explicitly specify table name model.
Comments
Post a Comment