user2206030
user2206030

Reputation: 69

Is it possible to render a different page when user is signed in? Rails 4 devise

I am trying to render a different home page when a User logs in, but Devise keeps rendering the same page. Is it possible to change this? I know you can do it directly in the home page source code, but that seems long and inefficient. For example, I know how to check if a user is logged in, but how would I render a different view when they are logged in?

Upvotes: 0

Views: 1206

Answers (1)

Matt
Matt

Reputation: 14038

You can set an after_sign_in_path to take logged in users to a specific page directly after logging in, if that's what you're after.

ApplicationController:

def after_sign_in_path_for(resource)
  some_path
end

Devise will automatically respect this.

More details

For a homepage that is different if you are logged in or not, you can either set your root_url action to redirect logged in users or you can change the link to the home page in the view.

1) Controller redirection (my preference)

SomeController
  def home_page
    if current_user
      redirect_to account_path
    end
  end
end

2) Or in views

<%= link_to 'Home', (current_user ? account_path : root_url) %>

Upvotes: 1

Related Questions