2017-09-21 2 views
0

devise를 사용하고 있으며 students 대시 보드에서 current_user.email을 호출하려고합니다. 하지만 동일한 "정의되지 않은 메서드`전자 메일 'nil : NilClass"오류가 계속 발생합니다. 사용자가 아닌 경우ruby ​​on rails "정의되지 않은 메소드`email for nil : NilClass"

학생/index.html.erb

<div class="student-index-header"> 
    <br><%= current_user.email %></br> 
    <div class="student-index-header-text"> 
    <a class="student-index-header-text-sign-out" href="<%= destroy_user_session_path %>">Sign-out</a> 
    </div> 
</div> 

student.rb

class Student < ApplicationRecord 
    belongs_to :user, optional: true 
    validates_presence_of :first_name, :last_name , :age, :description 
end 

user.rb

class User < ApplicationRecord 
    has_one :tutor 
    has_one :student 

# Include default devise modules. Others available are: 
# :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
end 
+1

'current_user'가 정의되어 있지 않은지 확인하십시오. 로드 했습니까? 로그인이 필요하지만 로그인하지 않았을 수 있습니다. – tadman

답변

0

고안은 current_user에 대한 전무을 반환 로그인했습니다.

,210

이 시도 :

... 

    <% if user_signed_in? %> 

    <br><%= current_user.email %></br> 

    <% else %> 

    authenticate_user! ##this redirects user to the login screen 
         ##and tells them they need to sign in to proceed. 

    <% end %> 
    ... 

당신은 그들이 로그인 후 다시이 페이지로 사용자를 리디렉션 할 수 있습니다. 그 방법을 배우려면 this answer.

+0

Ser Friendzone, 정말 고마워서 문제를 해결했습니다. –

+0

블레 이크를 돕기에 행복합니다! –

관련 문제