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.

On Thu, Dec 22, 2011 at 16:00, Tim S. [email protected] wrote:

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


Dave A., President, Dave A. Software Engineering & Training
Ruby on Rails (and others languages) Freelancer (NoVa, DC, or Remote)
On the web: www.DaveAronson.com, www.Codosaur.us, and www.Dare2XL.com
Specialization is for insects. (Heinlein) Have Pun, Will Babble! (me)

On 22 December 2011 21:08, Dave A.
[email protected] wrote:

On Thu, Dec 22, 2011 at 16:00, Tim S. [email protected] wrote:

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.

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.

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 P.

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 P.,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 P.

Are you kidding me?

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

2011/12/23 Gautam P. [email protected]

login page.

class ApplicationController < ActionController::Base

To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


thiagocifani

twitter.com/thiagocifani
del.icio.us/thiagocifani
http://del.icio.us/thiagocifani

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

2011/12/23 wei jack [email protected]

I want
Posted via http://www.ruby-forum.com/.


thiagocifani

twitter.com/thiagocifani
del.icio.us/thiagocifani
http://del.icio.us/thiagocifani

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.

Gautam P., 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.

On Fri, Dec 23, 2011 at 13:47, wei jack [email protected] wrote:

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


Dave A., President, Dave A. Software Engineering and Training
Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote)
DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!)
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me)

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