Rails: displaying a different view for the same url?

First, an example of what I'm trying to do:

  • If you go to http://www.meetup.com and you are not logged in, you will see a page that shows "Do something" .. "etc., which is a public page.
  • But when you are logged in, the same page (URL) shows "Welcome, xxx ... What's going on ..." etc., which is specific to you.
    This is what I am trying to do in my application.

How to do it in Rails 2.3.8?

So far I have had:

  • AboutsController designed to serve semi-static pages (preferably “O” is not multiple!)
  • Root route map.root => :controller => "about" .

Now that the non-logged-in-user goes to http://www.abc.com , it will get the contents of the about/index view. So far so good.

But when the user is logged in, I want the products/index view to display for the same URL, i.e. http://www.example.com URL (and not http://www.example.com/products )

Is this possible in Rails 2.3.8?

+4
source share
3 answers

The cleanest way to do this is with something like before_filter. This is the command at the top of your controller that gets called before any action on that controller (which you want). As a rule, you need to perform the same check for several actions in the controller, so it makes no sense to insert this check into each action directly.

Let's say you have a comment controller, and you want the editing, updating, and killing actions to be something that only a user registered in the system can do. This is very common. Let's look at an example. For brevity, I will not describe all the actions of the controller, just unique things:

 class CommentsController < ApplicationController before_filter :user_logged_in?, :only => [:edit, :update, :destroy] # all your actions would go here - index, show, etc # protected def user_logged_in? redirect_to dashboard_path unless current_user end end 

In the above example, the user_logged_in? will be launched before the actions for editing, updating or destruction are performed. If a render or redirection is called in this method, the rails stop and never trigger an action. Therefore, he called the filter. Instead, a render or redirect request will be executed.

The current_user method is a general helper that gives you most user authentication plugins, which is usually zero if there is no current user. Thus, our filtering method says that the rails are redirected to a specific path if the user is not logged in.

These are de facto rails to handle something like that. Very good question.

+3
source

It is certainly possible. You can display any views that you need conditionally:

  def index if current_user render :action => 'products', :controller => 'index' else render :action => 'index', :controller => 'about' end end 

Assuming you authenticate using Authlogic, Devise. No matter what logic you use to determine if the user is logged in, you are in a conditional expression.

+2
source

I saw how many websites handle this with redirects, say /dashboard , to make their application as clean as possible. No need to think too much about the fact that it is still the root URL, although this is clearly possible, as other solutions indicate :)

+1
source

All Articles