user2109855
user2109855

Reputation:

How to Redirect using Devise

So i've built my first app using Devise! I'm pretty stoked, but I would like to know how one goes about having the app re-direct to a specific page after logging in?

In other words,

Instead of logging in, and remaining at the home page, how do I get rails to redirect to a microposts page for example?

In my case specifically it only redirects to the posts page sometimes, and other times it just stays at the initial home page.

Here is my posts controller:

class PostsController < ApplicationController
 before_filter :authenticate_user!, :except => [:show, :index]

  def posts
  @title = "Posts"
  end
end

Upvotes: 2

Views: 82

Answers (1)

Jason Kim
Jason Kim

Reputation: 19031

By default, devise redirects you to the root, you can customize after_sign_in_path_for method anyway you like. There's also after_sign_out_path_for method at your disposal to customize.

ApplicationController < ActionController::Base
  # extra stuff

  def after_sign_in_path_for(user)
    if something
      posts_path
    else
      root_path
    end
  end

  def after_sign_out_path_for(user)
    new_some_other_path
  end
end

Upvotes: 2

Related Questions