Skip to main content

Redirect To Current Page After Login | GoRails - GoRails

General • Asked by Chris Oliver

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

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

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

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 https://www.nitrous.io/ and https://c9.io/

Manuel Quintanilla

I've found http://liveweave.com/ built with c/c++ most likely. We won't necessarily be running the editor in our rails app, however we need to pull information from it. So would I need to build a ruby extension to retrieve information from the C/C++ application like user information? Trying to wrap my head around it...

Normally you would just make HTTP requests to something like this if it has an API. It doesn't look like this has one, and it's probably not written in C because that's just too much work for building a website generally. This is probably mostly JS and some server side language like PHP, Ruby, or Python.

Manuel Quintanilla

Thanks!


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


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.