2010-02-11 10 views
0

나는 official Authlogic tutorial을 따라 갔지만 어떤 것이 나타났습니다. 나는 그 전체 생각Authlogic 튜토리얼에 이어 : # <UserSession : {: unauthorized_record => "<protected>"}>에 대한 정의되지 않은 메소드`email '>

# user.rb 
class User < ActiveRecord::Base 
    acts_as_authentic do |c| 
    c.crypto_provider = Authlogic::CryptoProviders::BCrypt 
    c.require_password_confirmation(false) 
    end 
    attr_accessible :username, :email, :password 
end 

# users_controller.rb  
def show 
    @user = @current_user 
end 

# application_controller.rb 
private 
    def current_user_session 
    return @current_user_session if defined?(@current_user_session) 
    @current_user_session = UserSession.find 
    end 

    def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session and current_user_session.user 
    end 

:

나는 성공적으로 사용자를 생성,하지만 난 show에 자신의 프로필 페이지를하려고 할 때, 나는 다음과 같은 오류 얻을 :

undefined method `email' for #<UserSession: {:unauthorized_record=>"<protected>"}> 

여기에 관련 코드의를 체인. 그리고 여기에보기가 있습니다 :

# views/users/show.html.erb 
<%=h @user.username%> 
email: <%=h @user.email %> 
<%= link_to "Edit", edit_account_path %> 

"전자 메일"줄을 제거하면 "편집"링크 만있는 페이지가 나타납니다. 사용자 이름이 나타나지 않습니다. 그러나 <%= debug @user %>을 추가하면 무슨 일이 일어나는지 정확히 알 수 있으며 데이터베이스에 저장된대로 emailusername의 값을 올바르게 인쇄합니다. @user.username이 왜 인쇄되지 않는지 전혀 알지 못하며 @user.email을 인쇄하려고 할 때 오류가 발생합니다.

show.html.erblayout of the tutorial은 을 사용하며 작동합니다. 방금 "로그인"을 "사용자 이름"으로 변경했습니다.

다음
undefined method `email' for #<UserSession: {:unauthorized_record=>"<protected>"}> 

사용한 edit 레이아웃과 부분적인 형태이다 : 다시

# views/users/edit.html.erb 
<h1>Edit My Account</h1> 

<% form_for @user, :url => account_path do |f| %> 
    <%= f.error_messages %> 
    <%= render :partial => "form", :object => f %> 
    <%= f.submit "Update" %> 
<% end %> 
<br /> 
<%= link_to "My Profile", account_path %> 

#views/users/_form.erb 
<%= form.label :username %><br /> 
<%= form.text_field :username %><br /> 
<br /> 
<%= form.label :email %><br /> 
<%= form.text_field :email %><br /> 
<br /> 
<%= form.label :password, form.object.new_record? ? nil : "Change password" %><br /> 
<%= form.password_field :password %><br /> 
<br /> 

모두에서 그대로 촬영 나는 "편집"을 클릭하면

은 또한, 나는 같은 원래의 오류 튜토리얼, 그냥 작동합니다.

필자는 Rails 2.3.2와 최신 Authlogic (현재 설치된 버전이 무엇이든간에)을 실행하고 있으며 피곤하고 좌절 한 두뇌가 말할 수있는 한 데이터베이스에 필요한 모든 필드가 있으며 they are spelled correctly.

무슨 일 이니? CURRENT_USER 실제로 방법이 아닌 인스턴스 변수 @current_user 것을

# users_controller.rb  
def show 
    @user = current_user 
end 

주의 사항 :

답변

1

음,이

그것은 문제가 자연 언어에 대한 내 취향이었다 밝혀 ... 당황. 내 경우에 적용 그러나, 나는 and에 대한 &&을 교환하고, as a resultcurrent_user_session.user이 고려되지 않는 한

def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session && current_user_session.user 
end 

다음은 튜토리얼에 배치로 current_user 방법이다. 그렇기 때문에 계속 UserSession이라고 말하고 있습니다. 나는 그것이 이상하다고 생각했지만 그것이 어디서 왔는지 알 수 없었다. 이제 나야.

0

내가 대신 다음과 같은 코드를 갖고 싶어 생각합니다.

+0

'current_user' 메소드는'@current_user'를 리턴합니다. 하지만 어쨌든 그것을 시도하고 아무것도 바뀌지 않았다. 하지만 고마워. –

1

지금 당신은

UserSession.create u 

UserSession.create :email => u.email, :password => u.password 

없는 큰 문제가

에 교체 할 수 있습니다.

하지만 다른 방식으로 저를 붙 잡았습니다. 내 사용자가 활성화 된 것을 잊었습니까? == 생성시 false. 나는 그것을 true로 설정했고 세션이 만들어졌습니다.

관련 문제