how to change Root_path after user sign in ?

I set root :to => welcome#index in routes.rb.

But I want after user successful login, set root_path to others.

FYI : i'm using Devise.

You could just have your welcome#index action redirect to another controller/action if the user is signed in.

That will work nicely for most purposes, but also means that logged-in users can't access the regular root page. Just have to be careful not to have anything they might need, *only* on there.

-Dave

It may work better the other way around, make the default root be the page for logged in user and redirect to welcome page if not logged in.

Colin

Thank you tim,dave,colin.

I get it. but find difficult to implement it. could you guy teach me how to write the function ? I'm new to ror.

Thanks.

Are you kidding me?

Suppose you need projects#index to be visible only to logged in users.

As Colin explained above, In your routes.rb put

root :to => projects#index

Devise provides you a method called authenticate_user! It checks if user is logged in, if not it redirects the user to login page. In the ProjectsController add a before_filter like this.

class HomeController < ApplicationController before_filter :authenticate_admin!

def index … end

… end

So if the user is logged in he sees the page or else he is redirected to the login page.

Gautam Pai

Follow Tim solution, Now I did.

root :to => welcome#index

class ApplicationController < ActionController::Base     before_filter :authenticate_user!, :except[:index, :show] end

In my welcome controller index   def index       if user_signed_in?         redirect_to projects_url       end    end

I would like to do like colin explained.

  root :to => project#index

but how to show the welcome#index as root if user not signed in ?

Gautam Pai,Thank for reply. :slight_smile: Using ur explaination.. when user not signed in. localhost:3000 it will go to login page.

I want it go welcome#index before user choose to login. Correct me, if I misunderstand ur explanation.

Then do

root :to => welcome#index

move authenticate_user! from ApplicationController to ProjectsController

then when user is logged in and visits welcome#index he will be redirected to projects_url.

If user is not logged in and visits projects url hel will be redirected to login page.

Gautam Pai

You could use the devise helper methods like

after_update_path_for

after_sign_in_path

I am not sure if this is the method’s name, please check the documentation for better understating.

thanks

follow this link https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out

Gautam Pai, I think can't move authenticate_user to ProjectController because I have other controllers actions which only registered user can access.

Thiagocifani, Thanks for helping me figure out to solve my problem. I tried. after_sign_in_path - redirect to a path after user success signed in. after_sign_out_path_for - redirect to a path after user signed out.

I want

Before user login, request localhost:3000. it will bring user to welcome#index. On welcome#index has a link go to sign in path.

After user success login. localhost:3000. will bring user to projects#index and during time user still signed_in, she/he can't access to welcome#index.

I am not understanding what you want to do! You want user log in your site twice?

what i want to do is like u go to facebook.

Before u login

www.facebook.com, u see the welcome page.

After u login

www.facebook.com, u see the page where all friends sharing stuffs.

But they're using the same URL both times. It's not a redirect, or changing the root path, but different content. You can do pretty much anything you want based on whether the user is logged in. It seems rather hackish, but you could have your controller send an entirely different set of vars and have the view use an entirely different section. You could at least keep it somewhat clean by separating the two views into partials or some such....

-Dave

You don't necessarily have to redirect either, you can just render a different layout if you want: