我正在做ruby on rails教程书,我发现了一个错误。我检查了他写的所有内容,但我仍然得到错误。它告诉我authenticate是一个未定义的nil方法:NILclass。我不知道该怎么做。
user.rb:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
attr_accessor :password, :password_confirmation
has_secure password
before_save { |user| user.email = email.downcase }
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, presence: true, length:{ minimum: 6 }
validates :password_confirmation, presence: true
end
和(某些)我的user_spec.rb
before do
@user = User.new(name: "Example User", email: "User@example", password: "foobar", password_confirmation:"foobar")
end
subject { @user }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
# USER.PASSWORD
describe "when password is not present" do
before { @user.password = @user.password_confirmation = " "}
it {should_not be_valid}
end
describe "when password is not present" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
describe "when password is nil" do
before { @user.password_confirmation = nil }
it {should_not be_valid}
end
describe "with a password that's too short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should 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
end
end
非常感谢所有有帮助的人。
答案 0 :(得分:1)
我是ruby on rails的新手。我只是通过了这个教程。当我在做的时候,我发现了同样的错误。
我希望我的解决方案可以帮到你。
你的user.rb中的
删除行:attr_accessor并将has_secure密码编辑为has_secure_password(添加下划线)
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
enter code here
before_save { |user| user.email = email.downcase }
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, presence: true, length:{ minimum: 6 }
validates :password_confirmation, presence: true
end
您的user.spec.rb 中的
1)您错过了 2)更改测试 3)最后,如果您想测试通过身份验证,您应该使用.com或与user.rb中的VALID_EMAIL_REGEX匹配的任何内容更改您的电子邮件结尾,如: 我希望这对你有所帮助。describe "when password is not present"
describe 'with a password that's too short'
从it { should be_valid}
到it { should be_invalid }
我认为如果密码小于5,它应该无效。before do
@user = User.new(name: "Example User", email: "User@example.com",
password: "foobar", password_confirmation:"foobar")
end