![](/img/trans.png)
[英]Rspec/Rails3: NoMethodError: undefined method `text' for nil:NilClass
[英]Authlogic + Rails3 - undefined method `Login' for nil:NilClass
我是Rails的新手,因此決定從Rails3開始。 經過大量搜索后,ive設法獲得了Authlogic的支持。 我可以注冊用戶,登錄和注銷。
現在,我想添加更多功能,獲得更多的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)))'
controllers / 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
型號/user.rb:
class User < ActiveRecord::Base
acts_as_authentic
end
controllers / 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 ..似乎可以使用Rails 3進行操作。
用戶尚未登錄,因此@user為nil。 像在示例Rails3應用程序中看到的那樣放置一些邏輯。
請注意,我正在使用該分支用於rails3的authlogic,盡管我不確定它是否比main分支更好或更糟。
http://github.com/odorcicd/authlogic/tree/rails3
另請注意,我在railscast 160上嘗試過rails3,但是已經過時了。 我將Railscast用作基本方向比較幸運,然后堅持使用上面github上trevmex的示例應用程序進行實際實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.