问题
I had the official authlogic plugin running perfectly on Rails 2.3.5. I converted my application to Rails 3.0.1 and now I'm having some problems.
I've included the following authlogic gem in my gemfile
gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'
When a user logs in, the session is saved. When calling that user session, the returned value is nil. UserSession.find returns a nil value
so I'm not able to assign a current_user.
sessions_controller.rb
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save!
flash[:notice] = 'Login successful'
redirect_to root_url
else
render :action => 'new'
end
end
application_controller.rb
helper_method :current_user, :current_user_session
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 && current_user_session.record
end
When a user logs in, the flash notice is returned with 'Login sucessful'
but the current_user isn't set. I've also tried this with the official authlogic plugin and nothing changed. Am I missing something here?
Thanks!
Tim
回答1:
I had this exact problem. For some reason it was interfering with basic auth - to fix it I set allow_http_basic_auth to false.
class UserSession < Authlogic::Session::Base
allow_http_basic_auth false
end
来源:https://stackoverflow.com/questions/4067641/not-able-to-set-a-current-user-using-authlogic-on-rails-3-0-1