views:

365

answers:

3

Hey,

please help me to understand something. In Authlogic example in UsersController it's always used @current_user, so for instance:

def show
  @user = @current_user
end

(taken from http://github.com/binarylogic/authlogic_example/blob/master/app/controllers/users_controller.rb)

Why is that? In my controllers I use just current_user instead of @current_user.

And besides - Authlogic works perfectly for me, but I don't see magic columns being populated (like last_login_at or last_login_ip). Should I initialize them somehow specifically besides just adding into migration?

UPD After some investigation, I found that if there're only fields last_login_at and last_login_ip from "Magic fields", then they will not be populated. If I add a full set of magic fields, it is working perfectly.

UPD2 My concern regarding current_user is only about UsersController: why does it have @current_user and not current_user?

A: 

current_user is typically a method defined in app/controllers/application_controller.rb which sets the @current_user instance variable if it is not already defined -- here is an example:

  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

Re the "magic columns", these should be set by Authlogic automatically. For example, if your user sessions controller logs in a user:

@user_session = UserSession.new(params[:user_session])
@user_session.save

Authlogic should write the last_login_at and last_login_ip attributes for you. More info in the Authlogic docs under Module: Authlogic::Session::MagicColumns

zetetic
Yes, current_user is a method defined in ApplicationController. That doesn't answer why UsersController use @current_user though. As for last_login_at and last_login_ip - is there any setting for development environment not to populate them? I set them up exactly as per example.
Vitaly
current_user is a helper method which keeps track of the user currently logged in. @current_user is an instance variable interacting with the model. They are completely different.
Shripad K
I understand the difference. What I'm not getting is how @current_user is being initialized in UsersController?
Vitaly
UsersController inherits from ApplicationController, so it gets its methods. When you call `current_user` from UsersController (or UserSessionsController) you're calling the method defined in your `application_controller.rb`.I'd check the code sample you're using -- it may be incomplete or out of date. As Jens suggested, you ought to be calling `current_user` instead of using the instance variable, unless you know `@current_user` is being set somewhere else in your code.
zetetic
This is what I thought as well and was surprised to see @current_user in the code instead of current_user. This is not my code, this is official example of authlogic.
Vitaly
A: 

I think the code from the example isn't a really good example.

You shouldn't use @current_user to set the @user variable. Because it won't work if the ApplicationController#current_user method isn't called before show action of the UserController. Basically they are both exactly the same after current_user is called once.

the User Controller should look like this

class UserController < ApplicationController
  def show
    @user = current_user
  end
end

As for the Magic Columns I have no Idea why they don't work for you.

jigfox
Yes, this is what I thought. However, the code above works and it is standard example for authlogic. This is not my code.
Vitaly
yes it is in the example, but it is a bad example it should be @user = current_user
jigfox
+1  A: 

As for last_login_at and last_login_ip, do you have current_login_at and current_login_ip fields in your table ? last_login_at and last_login_ip are set with the values of current_login_at and current_login_ip before they are reset.

Cecile
Ah, that makes sense. I wrote in UPD that with full set of magic fields it worked. Now it's making sense to me! Thanks :) They should have added this into documentation.
Vitaly