I am currently using Devise to authenticate users on my app. It's working great for the most point, but I am having trouble with a specific action:
view:
<p id="save"><%= link_to "Save", new_save_path, :remote => true %></p>
saves_controller.rb:
def new
  if user_signed_in?
    @save = Save.create(:user_id => current_user.id)
    render :update do |page|
      page.replace_html "save", "Saved!"
    end
  else
    redirect_to new_user_session_path, :notice => "You need to sign in to do that."
  end
end
As you can see, because the action is an ajax one, I can't use the traditional before_filter :authenticate_user! method.  So instead I am redirecting the user to the sign in page.  
The problem is, I want to automatically redirect the user back to the previous page when they logged in.
I understand I can do this with the session[:"user.return_to"] but I'm having trouble setting it. How can I do this? Or am I going about this all wrong?
I believe the session key is :"#{scope}_return_to, which will be simply :user_return_to for a User class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With