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