views:

53

answers:

1

I followed the tutorial exactly: http://github.com/binarylogic/authlogic_example

But I am wondering how can I get the login form to appear on all pages when someone isn't logged in?

I searched Google and looked at many forums for answers, but the only solutions I could find require me to define @user_session = UserSession.new for every page that has the login form.

What I'd like to do is put the form in the application.html.erb file and only have it show up when people are logged out (which I've managed to do).

However, I'd like to only have to put @user_session = UserSession.new in one controller, such as the ApplicationController, as it is very non-RESTful to have to put that piece of code on every single page. If I leave the form code in the application.html.erb and that code is not defined for a page in the controller, then the page will give an error.

I don't want to have to define it for every page, as it will be a lot of work to have to remove it from every single page if I ever decide to get rid of the login form or something.

In short: I want to put the Login form on every single page of my site, and only have to put @user_session = UserSession.new in one controller.

Thank you.

+1  A: 

In ApplicationController, you can do the following:

before_filter :prepare_new_session

def prepare_new_session
  @user_session = UserSession.new if current_user.blank?
end

That'll give you a @user_session object for every action (if a user is not currently logged in). That way, you can include the form on every page by placing it in the layouts/application.html.erb file.

vegetables
Thank you. This is exactly what I needed. :)
Felix