Ask A Question

Notifications

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)
end
super(opts)
end

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

Reply

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

Reply
Jordano Moscoso Jordano Moscoso

Thanks, i was wondering what was going on.

Reply

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?

Reply

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.

Reply

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.

Reply

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.

Reply

@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

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

Reply

There are, although I haven't personally used any so I can't confirm if they're any good. You can check out like https://www.nitrous.io/ and https://c9.io/

Reply

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

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.