2013-03-18 1 views
0
에서 제공하는 방법을 발견 레일 콘솔은

내 문제는 내가 current_user = user.authrnticate(foobar)을 시도하고 다시 "레일 C"를 사용하거나 corectly user.rb 파일을 볼되지 믿고 날 리드 NameError: undefined local variable or method 'user' for main:Object는 "정의되지 않은 지역 변수 또는 메소드"는 has_secure_password

을 얻었다.

[rake db : test : prepare] 및 [rake db : migrate]를 수행하기 전후에 레일스 서버를 다시 시작해 보았습니다.

[rails c]는 user.rb에 정의되지 않은 메소드를 실행합니다.

Rspec이 문제없이 실행 중입니다. user_spec의 모든 테스트가 통과합니다. 그리고 내가이라고 말하면 모든 테스트가 실패합니다.

require 'spec_helper' 

describe User do 

before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
         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(: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 to long" do 
    before { @user.name = "a" * 51 } 
    it { should_not be_valid } 
end 

describe "when email format is invalid" do 
    it "should be invalid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] 
             [email protected]_baz.com [email protected]+baz.com] 
     addresses.each do |invalid_address| 
      @user.email = invalid_address 
      @user.should_not be_valid 
     end 
    end 
end 

describe "when email format is valid" do 
    it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] [email protected]] 
     addresses.each do |valid_address| 
      @user.email = valid_address 
      @user.should be_valid 
     end 
    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 "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
end 

describe "when password is too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5} 
    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 
end 
end 

내 user.rb 내가 액티브 문서의 읽기 고투를 포함한 솔루션을 철저하게 보았다 내가 난처한 상황에 빠진거야

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation 
    has_secure_password 

    before_save { |user| user.email = user.email.downcase } 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][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 

and here is the entire project on github

입니다.

도움을 주시면 대단히 감사하겠습니다.

답변

0

많은 도우미 메서드를 콘솔에서 호출 할 수 없습니다.

당신은 종종 helper.helpername를 통해 그들을 호출하여 액세스 할 수 있지만 이것은 항상

어떻게 데이터베이스에서 사용자를 받고 일을하지 않는 이유는 무엇입니까? 예를 user = User.find_by_name('name')

+0

그게 전부입니다! 가변 사용자를 인스턴스화하지 않았습니다. 고마워요, 고마워요! – Kit

0

를 들어

이 오류는 :

NameError: undefined local variable or method 'user' for main:Object 

당신이 변수 사용자를 인스턴스화하지 않았다고 말한다.

사용자 변수에있는 데이터베이스에서 사용자를 가져 왔습니까?당신은 데이터베이스의 사용자가있는 경우

확인하려면 :

귀하의 IRB 세션은 다음과 같이 보일 것이다

> User.all 

당신은

> user = User.find_by_name("Jon") 
> current_user = user.auhenticate("valid_password") #will work 
"존"이라는 데이터베이스에서 유효한 사용자가있는 경우

데이터베이스에 사용자가없는 경우 :

> User.create(name: "Jon", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar") 
> user = User.find_by_name("Jon") 
> current_user = user.auhenticate("valid_password") #will work 

RSpec은 테스트 데이터베이스를 사용하지만 기본적으로 레일 c는 개발 1을 사용하므로 레일 c에서 작업 할 때 개발 데이터베이스에 레코드가 있는지 확인하십시오.

rails c 명령이 모든 프로젝트 라이브러리를로드 중입니다.

+0

그게 전부 야! 가변 사용자를 인스턴스화하지 않았습니다. 정말 고맙습니다! – Kit