开发者

Rails + Authlogic @current_user problem - "Show" method no longer works

开发者 https://www.devze.com 2023-02-06 15:08 出处:网络
I implement开发者_开发知识库ed Authlogic in my rails app which works and tests great. Users can login, logout, etc.

I implement开发者_开发知识库ed Authlogic in my rails app which works and tests great. Users can login, logout, etc.

However, since I changed my methods in the users_controller to use the @current_user variable, none of my user methods or forms (other than "index" strangely...) work!

For example, this "Show" methods works and shows the user profile for any user (Note: I want to eventually restrict this to the currently logged in user somehow):

def show
    @user = User.find(params[:id])
end

If I follow the show method as shown in the Authlogic rails cast and the official example to:

def show
    @user = @current_user
end

I get all kinds of undefined method METHOD for nil:NilClass errors. It seems as the though the @current_user variable doesn't contain any of the user's information.

I assume the @current_user is pulling info from the current_user method as defined in the application controller below:

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

What am I missing? My hunch is that there is no mapping between the User object from the user model and the current_user method. Any help would be greatly appreciated!

Thanks in advance!

~Dan


Shouldn't it be just

def show
    @user = current_user
end

without the @?


current_user is a helper that comes with authlogic, so you have to use it without '@'. Usually you create @sothing to pass it from controller to view, so you will access something you prepared in the action method.


I had a similar problem with authlogic and rails 3, it was because of the apache http authentication that i had on my production server. And i got that fixed by adding the below line to my user_session.rb(model)

    allow_http_basic_auth false

this is the blog link that saved my life phew http://www.christopherirish.com/2011/08/12/rails-3-authlogic-sessions-not-persisting-in-production/comment-page-1/#comment-181


def
if @current_user
 @user = @current_user
else
 @user = User.find(params[:id])
end
end

Just a guess, give a try on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消