Vitaly
Vitaly

Reputation: 2585

Authlogic - current_user or @current_user?

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?

Upvotes: 0

Views: 2644

Answers (3)

Cecile
Cecile

Reputation: 11

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.

Upvotes: 1

jigfox
jigfox

Reputation: 18177

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.

Upvotes: 0

zetetic
zetetic

Reputation: 47548

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

Upvotes: 1

Related Questions