brad
brad

Reputation: 9773

The most Rails way of serving up a static home page if not logged in

I'm experimenting with a few ways of doing this but just thought I'd ask the gallery.

I want the root page of my app (/) to be one of 2 things depending on whether a user is logged in or not.

If they are not logged in I want to dish up a static page with some static info and a login form.

If they are logged in I want them to be served up the main index page of my app (/invoices/).

I am using authlogic to manage user login.

Some of the ideas I've had for doing this seem unnecessarily convoluted (and this is Rails - it should be simple shouldn't it?) and I figure someone already knows a better way.

Upvotes: 0

Views: 193

Answers (3)

Zach Inglis
Zach Inglis

Reputation: 1257

I think this answers your question: Protecting Content with AuthLogic

It is the cleanest way of what you are looking for, and the standard way.

Upvotes: 1

ranendra
ranendra

Reputation: 2512

I would add the following method definition to a controller to define a layout over there. If the behavior would be global then application_controller would be the place for this method definition.

layout :choose_layout

def choose_layout
    if current_user
      return 'user_view'
    end
    return 'non_user_view'
 end

Upvotes: 1

Trip
Trip

Reputation: 27114

homepage_controller

def index
 respond_to do |format|
  if current_user
    format.html { render :partial => 'view', :layout => 'nonuser' }
  elsif !current_user
    format.html { render :partial => 'view', :layout => 'user' }
  end
end

Something like that?

Upvotes: 1

Related Questions