Question

If you go to Twitter.com when you are not logged in, you will see the marketing page with the login & registration fields.

However, if you go there when you are logged in, you will see your activity stream for your Twitter handle.

I know one way to do this in Rails is to have a home/index for the logged in users, and just use public/index.html for the marketing site. But Twitter is not using public/index.html, so I am wondering how they do it?

Is it just a simple case of having one root route, but then an if statement that displays two different pages depending on whether or not the user is logged in?

Or is there some other more fancy routing trick that I can use to do that?

If it is just an if-statement, that seems a bit hacky...no?

Thanks.

Was it helpful?

OTHER TIPS

By default controller methods that handle specific routes render a view that has a corresponding name (home/index route links to home controller and index action which in turn renders the view index.html.erb inside the app/views/home folder).

However, inside the controller method that handles the root request you can simply render a different view based on the fact that a user is logged in or not.

You would probably have something like this:

class HomeController
  def index
   ...
   if user_logged_in?
      render "logged_in_user"
   # else it will render the index view by default
   end
end

end

In this case "logged_in_user" would be a different view (template or html.erb file) from the app/views/home folder.

For more information take a look here: Rails rendering guide

it can some kind of redirect for not logged in users to different controller , checking of user session can be done in before_filter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top