Ruby on rails Rails,未定义的方法错误。但我可以在rails控制台中使用此方法
在我的rspec测试中。我得到了一个命名者: 未定义的方法'password_digest=' 但是在rails控制台中。我确实可以使用.password\u摘要方法 我很困惑。我搜索了谷歌。关于这件事有很多线索。即使在堆栈溢出中。但这些都没用。我的数据库中有密码摘要字段 我甚至可以设置我的密码 见: 还有,在我的应用程序中。一切似乎都很好。真奇怪。 请帮忙Ruby on rails Rails,未定义的方法错误。但我可以在rails控制台中使用此方法,ruby-on-rails,Ruby On Rails,在我的rspec测试中。我得到了一个命名者: 未定义的方法'password_digest=' 但是在rails控制台中。我确实可以使用.password\u摘要方法 我很困惑。我搜索了谷歌。关于这件事有很多线索。即使在堆栈溢出中。但这些都没用。我的数据库中有密码摘要字段 我甚至可以设置我的密码 见: 还有,在我的应用程序中。一切似乎都很好。真奇怪。 请帮忙 1.9.2-p290 :002 > User.first.password_digest User Load (0.3ms)
1.9.2-p290 :002 > User.first.password_digest
User Load (0.3ms) SELECT "users".* FROM "users" LIMIT 1
=> "$2a$10$NCfX2SgKeqGARJ68StxRJuCbbbK7g18n5FPxbHY5THwg4pAdHUvui"
1.9.2-p290 :003 >
[5]+ Stopped rails console
luke@Macbook-Pro~/Documents/workspace/RoR/rails_projects/sample_app2012$ bundle exec rspec spec/models/user_spec.rb
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in #<Class:0x000001029f6688> instead. (called from <top (required)> at /Users/luke/Documents/workspace/RoR/rails_projects/sample_app2012/spec/models/user_spec.rb:14)
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in #<Class:0x000001029f6688> instead. (called from <top (required)> at /Users/luke/Documents/workspace/RoR/rails_projects/sample_app2012/spec/models/user_spec.rb:14)
FFFFFFFFFFFFFFFFFFFFF
Failures:
1) User
Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
NoMethodError:
undefined method `password_digest=' for #<User:0x00000100beee60>
# ./spec/models/user_spec.rb:17:in `new'
# ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'
..... There are 19 more failures, to save space, i didn't paste them here.
这是modles下的my user.rb:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
class User < ActiveRecord::Base
has_secure_password
attr_accessible :name, :email, :password, :password_confirmation
validates :name, presence: true, :length => { maximum: 50 }
valid_email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
:format => { with: valid_email_regex },
:uniqueness => { case_sensitive: false }
validates :password, length: { minimum: 6}
end
您可以在控制台中使用User.first.password\u digest,因为password\u digest和password\u digest=的方法不同
密码\u摘要=与以下内容相同:
否则,密码摘要:
您可以在控制台中使用User.first.password\u digest,因为password\u digest和password\u digest=的方法不同
密码\u摘要=与以下内容相同:
否则,密码摘要:
类用户有方法密码但没有方法密码摘要= 这是不同的方法,所以用户实例对密码不负责任
它应该返回密码摘要,但不应该响应密码摘要,我认为类用户有方法密码,但没有方法密码摘要= 这是不同的方法,所以用户实例对密码不负责任
它应该返回password\u digest,但不应该响应password\u digest。我想,在您的控制台中,您正在尝试password\u digest方法,而不是password\u digest=方法。您能够在控制台中执行以下操作吗?User.first.password_digest=$2a$10$ncfx2sgeqgarj68stxrjucbk7g18n5fpxbhy5thwg4padhuvui
我认为您的问题可能根源于这样一个事实,即您在用户模型中只指定某些属性为attr_可访问。尝试将:password\u digest添加到该列表中,并查看您的测试是否通过。在控制台中,您正在尝试password\u digest方法,而不是password\u digest=方法。您能够在控制台中执行以下操作吗?User.first.password_digest=$2a$10$ncfx2sgeqgarj68stxrjucbk7g18n5fpxbhy5thwg4padhuvui
我认为您的问题可能根源于这样一个事实,即您在用户模型中只指定某些属性为attr_可访问。尝试将:password\u digest添加到该列表中,看看您的测试是否通过。也只是停留在该列表上一段时间 别忘了
rakedb:test:在新迁移之后做好准备 也只是在这上面停留了一段时间 别忘了
rakedb:test:在新迁移之后做好准备 您好,我是RoR的新手,我学习了RailsTutorial,也遇到了这个问题。在谷歌搜索了很长时间后,我发现我的db/test.sqlite3还没有更新。这意味着在我的db/development.sqlite3中有“password_digest”,但在我的db/test.sqlite3中没有该列。
在我运行命令“rake db:reset”和“rake db:test:prepare”之后,问题就消失了。嗨,我是RoR的新手,我学习了RailsTutorial,也遇到了这个问题。在谷歌搜索了很长时间后,我发现我的db/test.sqlite3还没有更新。这意味着在我的db/development.sqlite3中有“password_digest”,但在我的db/test.sqlite3中没有该列。
在我运行命令“rake db:reset”和“rake db:test:prepare”之后,问题就消失了。我昨天遇到了完全相同的问题。开发和测试环境中的控制台工作良好。网络应用程序本身也很好用。但是,与所述问题几乎相同的测试没有方法错误而失败 我试图停止spring,重新创建测试数据库,杀死memcached。但它不起作用 最后,我在fixture文件test/fixtures/users.yml中添加了一些数据 它以前是空的。然后测试通过了。之后我将user.yml设为空。测试仍然通过
我还不知道为什么。但希望能有所帮助。我昨天也遇到了同样的问题。开发和测试环境中的控制台工作良好。网络应用程序本身也很好用。但是,与所述问题几乎相同的测试没有方法错误而失败 我试图停止spring,重新创建测试数据库,杀死memcached。但它不起作用 最后,我在fixture文件test/fixtures/users.yml中添加了一些数据 它以前是空的。然后测试通过了。之后我将user.yml设为空。测试仍然通过
我还不知道为什么。但希望能有所帮助。您可以在控制台中为用户设置密码摘要?是的,我可以。1.9.2-p290:005>user.password_digest=>2a$10$X8CSsstOqZKKA6qVHpW9.uh5lzd7dxfgcxvibeppcfbg8kfbd4y 1.9.2-p290:006>user.password_digest=1=>1.9.2-p290:007>user.password_digest=>1您可以在控制台中为用户设置密码_digest吗?是的。1.9.2-p290:005>user.password_digest=>2a$10$X8CSsstOqZKKA6qVHpW9.UH5LZD7DXFGNCAXIBEPCFBG8KFBD4Y 1.9.2-p29
0:006>user.password\u digest=1=>11.9.2-p290:007>user.password\u digest=>1这将丢失密码\u digest这只是解释差异这将丢失密码\u digest这只是解释差异
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
require 'spec_helper'
describe User do
before(:each) do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest)}
it { should respond_to(:password)}
it { should respond_to(:password_confirmation)}
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is valid" do
valid_addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
valid_addresses.each do |valid_address|
before { @user.email = valid_address}
it {should be_valid }
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { @user.password = @user.password_confirmation = " " }
it {should_not be_valid}
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by_email(@user.email) }
describe "with valid password" do
it { should == found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
describe "remember token" do
before { @user.save }
its(:remember_token) { should_not be_blank }
end
end
end
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
class User < ActiveRecord::Base
has_secure_password
attr_accessible :name, :email, :password, :password_confirmation
validates :name, presence: true, :length => { maximum: 50 }
valid_email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
:format => { with: valid_email_regex },
:uniqueness => { case_sensitive: false }
validates :password, length: { minimum: 6}
end
def password_digest=(digest)
@password_digest = digest
end
def password_digest
@password_digest
end
harry:
name: Harry
email: harry@example.com
password_digest: <%= User.digest('password') %>