Ask A Question


You’re not receiving notifications from this thread.

Redirect To Current Page After Login | GoRails - GoRails

Chris Oliver asked in General

For those who want similar behaviour on before_action :authenticate_user!

  def authenticate_user!(opts = {})
unless opts || user_signed_in? || devise_controller? || !redirectable?
store_location_for(:user, request.fullpath)

def redirectable?
request.get? && !request.xhr? && is_navigational_format?


FYI in the after_sign_in_path_for(resource) method I had to change
to Devise's:

Jordano Moscoso Jordano Moscoso

Thanks, i was wondering what was going on.


How do I achieve this redirect for any page and all links, rather than just on the 'Login' page? That's the major downside I see to this approach. I would have to setup a parameter for every link, right?


This doesn't make sense for a site that's globally locked down. That's why it's designed in the way that it is.

If you're locking down everything, the Devise before_action :authenticate_user! can take care of that for you. You can also put that in ApplicationController to have it apply to every single request.


Why not a simple cookie with current_path (set on every page), after sign_up or sign_in you simply get the cookie and redirect to it.


You don't necessarily always want to redirect to that same page, this way you can specify it as you want. Of course, you can set it up that way to always set a cookie. Depends on what you're looking to accomplish really.


@disqus_HFIU4QcJxV:disqus this would lead to issues if a user were to 'right-click + open' multiple pages in different browser tabs, the cookie would keep getting overwritten and would likely lead to unexpected results

Manuel Quintanilla Manuel Quintanilla

Hey Chris, are there any open source online code editors similar to Codepen I can integrate into my rails app and build upon it to work with my site dynamically.


There are, although I haven't personally used any so I can't confirm if they're any good. You can check out like and


It can be used to devise the latest version of 4.1.1?

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.