2010-10-20 38 views
0

Im新的Rails,並決定開始使用Rails3。經過大量搜索之後,我成功地獲得了一些Authlogic的工作。我可以註冊一個用戶,登錄&註銷。Authlogic + Rails3 - 未定義的方法`登錄'爲零:NilClass

現在,我想添加更多的功能,獲得更多的authlogic工作。我使用Railscast EP 160作爲我的參考。代碼

部分的教程扔發現錯誤: 如:

<!-- layouts/_topbar.erb --> 
<%= link_to "Login", login_path %> 

,我得到了以下錯誤消息:

undefined local variable or method `login_path' for #<#<Class:0x0000000311e8f8>:0x0000000310af38> 

爲了克服這個問題,我剛使用的字符串。即<%= link_to「登錄」,「/ UserSessions/new」%>

現在看來我已經陷入僵局。當我嘗試輸出當前用戶:

<%= @user.Login %> 

我收到一個錯誤,即時無法繞過。你能幫我麼?謝謝:)請在下面找到錯誤信息和一些代碼。

undefined method `Login' for nil:NilClass 

完整曲線讀[截斷]

activesupport (3.0.0) lib/active_support/whiny_nil.rb:48:in `method_missing' 
app/views/layouts/_topbar.erb:16:in `_app_views_layouts__topbar_erb__4536428193941102933_40950340__3781575178692065315' 
actionpack (3.0.0) lib/action_view/template.rb:135:in `block in render' 
activesupport (3.0.0) lib/active_support/notifications.rb:54:in `instrument' 
actionpack (3.0.0) lib/action_view/template.rb:127:in `render' 
actionpack (3.0.0) lib/action_view/render/partials.rb:294:in `render_partial' 
actionpack (3.0.0) lib/action_view/render/partials.rb:223:in `block in render' 
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `block in instrument' 
activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument' 

請求參數:無

我的Gemfile寫着:

gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3" 

的config/routes.rb文件:

resources :users 
    resources :user_sessions 
    resources :ibe 
    match ':controller(/:action(/:id(.:format)))' 

控制器/ application_controller.rb:

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 && current_user_session.record 
    end 

模型/ user_session.rb [獲取當前用戶..也從網上的例子作出的部分]:

class UserSession < Authlogic::Session::Base 
include ActiveModel::Conversion 
    def persisted? 
    false 
    end 
    def to_key 
    new_record? ? nil : [ self.send(self.class.primary_key) ] 
    end 
end 

模型/用戶。 RB:

​​

控制器/ users_controller.rb:

class UsersController < ApplicationController 
    before_filter :require_no_user, :only => [:new, :create] 
    before_filter :require_user, :only => [:show, :edit, :update] 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:notice] = "Account registered!" 
     redirect_back_or_default account_url 
    else 
     render :action => :new 
    end 
    end 

    def show 
    @user = @current_user 

    end 

    def edit 
    @user = @current_user 

    end 

    def update 
    @user = @current_user # makes our views "cleaner" and more consistent 
    if @user.update_attributes(params[:user]) 
     flash[:notice] = "Account updated!" 
     redirect_to account_url 
    else 
     render :action => :edit 
    end 
    end 


end 

好吧,我決定切換到Devise ..似乎開箱即用的軌道3 .. yaay!

回答

0

用戶未登錄,因此@user爲零。把你在例子Rails3應用程序中看到的一些邏輯放在這裏。

http://github.com/trevmex/authlogic_rails3_example/blob/master/app/views/layouts/application.html.erb

注意我用的這個分支的authlogic爲Rails3中,雖然我不知道這是否是比任何主枝更好或更壞。

http://github.com/odorcicd/authlogic/tree/rails3

另外請注意我試圖Rails3中與railscast 160,但它是過時的。我使用railscast獲得了更多的運氣,僅用於基本定位,然後在上面的github上使用trevmex中的示例應用程序來實際實現。

+0

謝謝彼得!我希望我已經停留了一會兒,看到你的答覆..但我只是放棄了authlogic,並與設計一起..似乎很好,直到現在。 – 2010-10-27 16:00:19

相關問題