我跟着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 %>
以查看正在進行的操作時,它會正確打印存儲在數據庫中的email
和username
的值。我不知道爲什麼它不打印@user.username
,爲什麼當我嘗試打印@user.email
時會引發錯誤。
應該指出的是,show.html.erb
layout 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'。但我仍然嘗試過,沒有任何改變。但是,謝謝。 – 2010-02-11 07:39:02