我正在將Rails 2應用程序遷移到Rails 3,並且必須使用新的身份驗證解決方案,因爲不再支持restful_authentication。我正在嘗試設計並且遇到問題。設計軌3:'user_signed_in?'假'signed_in?' true'current_user'nil
我有一個登錄頁面,然後指引您到應用程序。您也可以在不登錄的情況下使用該應用,但有些選項在登錄之前不可用。
一旦記錄在制訂說,我已經通過閃光消息登錄,但任何current_user
將評估爲nil
,current_user.blank?
評估爲true
任何user_signed_in?
將評估爲false
。奇怪signed_in?
評估爲true
。檢查會話數據顯示warden.user.user.id
包含正確的用戶標識並且存在csrf標記。在視圖和控制器中都是如此。
這裏的routes.rb
MyApp::Application.routes.draw do
devise_for :users
match "/:controller(/:action(/:id))"
match "/:controller(/:action(/:id))(.:format)"
match "/:controller(/:action(.:format))"
devise_scope :user do
get 'signup', :to => 'devise/registrations#new'
get 'login', :to => 'devise/sessions#new'
get 'signout', :to => 'devise/sessions#destroy'
end
root :to => 'main#design'
end
User.rb
require 'digest/sha1'
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable, :validatable,
:encryptable, :encryptor => :restful_authentication_sha1
attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_company, :profile_state, :profile_city, :profile_postcode, :profile_country, :profile_phone, :profile_address
validates_length_of :name, :maximum => 100
validates_presence_of :email
validates_length_of :email, :within => 6..100
validates_uniqueness_of :email
def profile_complete?
if !(self.profile_address.blank? || self.profile_city.blank? || self.profile_state.blank? || self.profile_phone.blank? || self.name.blank? || self.state != 'active')
true
else
false
end
end
end
我見過很多類似的問題對SO,但沒有一個答案似乎適合我的方案。任何幫助將不勝感激,謝謝!
編輯:warden.authenticate(:scope => :user)
返回我期望current_user
返回。