2010-02-11 25 views
0

我跟着official Authlogic tutorial,但出現了一些事情。以下Authlogic教程,但:未定義的方法`電子郵件'爲#<UserSession:{:unauthorized_record =>「<protected>」}>

我成功地創建了一個用戶,但是當我嘗試show他的個人資料頁,我得到以下錯誤:

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

下面是相關代碼:

# 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 

我認爲這是整個鏈。這裏的觀點:

# 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

發生了什麼事?

回答

1

好了,這是令人尷尬...

原來,問題是我對自然語言的偏愛。這裏的current_user一種方法,在教程佈局:

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

但是,它適用於我的情況的時候,我換了&&and,並as a resultcurrent_user_session.user沒有被考慮在內。這就是爲什麼它一直說UserSession這個和那個。我認爲這很奇怪,但看不到它來自哪裏。現在我知道了。

0

我想你想有下面的代碼來代替:

# users_controller.rb  
def show 
    @user = current_user 
end 

注意CURRENT_USER實際上是一個方法,而不是一個實例變量@current_user。

+0

'current_user'方法返回'@ current_user'。但我仍然嘗試過,沒有任何改變。但是,謝謝。 – 2010-02-11 07:39:02

1

現在可以替換

UserSession.create u 

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

沒什麼大不了的。

但是,這引起了我的其他方式。我忘記了我的用戶活躍? ==創建時爲false。我已將其設置爲true並創建了會話。

相關問題