私はレールを使用する初心者であり、モデルが has_secure_password を使用するコントローラーに対していくつかのテストを構築しようとしています。テストに従ってください:
require 'test_helper'
class UsersControolerTest < ActionController::TestCase
setup do
@dados_user = {
full_name: "Fulano",
bio: "lorem ipsun lorum lorem ipsun lorum lorem ipsun lorum",
location: "Brazil",
email: "fulano@gmail.com",
password: "123456",
password_confirmation: "123456"
}
end
test "Cria usuário banco" do
assert_difference('User.count') do
post :create, user: @dados_user
end
end
end
テストを実行すると、次のエラー メッセージが表示されました。
ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password: INSERT INTO "users" ("full_name", "email", "password", "location", "bio", "created_at"," updated_at", "id") ...
モデルのコードは次のとおりです。
class User < ActiveRecord::Base
has_many :rooms, :dependent => :destroy
has_many :reviews, :dependent => :destroy
scope :confirmed, where('confirmed_at IS NOT NULL')
has_secure_password
attr_accessible :bio, :email, :full_name, :location, :password,
:password_confirmation
validates_presence_of :email, :full_name, :location
validates_length_of :bio, :minimum => 30, :allow_blank => false
validates_format_of :email, :with => /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/
validates_uniqueness_of :email
before_create :generate_token
def generate_token
self.confirmation_token = SecureRandom.urlsafe_base64
end
def confirm!
return if confirmed?
self.confirmed_at = Time.current
self.confirmation_token = ''
save!
end
def confirmed?
confirmed_at.present?
end
def self.authenticate(email, password)
confirmed.find_by_email(email).try(:authenticate, password)
end
end
誰かが私を助けることができますか?前もって感謝します。
PS: 移行コードに従ってください
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :full_name
t.string :email
t.string :password
t.string :location
t.text :bio
t.timestamps
end
add_index :users, :email, :unique => true
end
end
以下の移行は、has_secure_password の使用に関して行われました。
class RenamePasswordOnUsers < ActiveRecord::Migration
def up
rename_column :users, :password, :password_digest
end
def down
end
end
class AddConfirmationFieldsToUsers < ActiveRecord::Migration
def change
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_token, :string
end
end