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

99封情书 提交于 2019-12-12 02:27:59

问题


I implemented 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


回答1:


Shouldn't it be just

def show
    @user = current_user
end

without the @?




回答2:


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.




回答3:


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




回答4:


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

Just a guess, give a try on it.



来源:https://stackoverflow.com/questions/4764002/rails-authlogic-current-user-problem-show-method-no-longer-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!