Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually setting return_to with devise

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?

like image 877
goddamnyouryan Avatar asked May 10 '11 23:05

goddamnyouryan


1 Answers

I believe the session key is :"#{scope}_return_to, which will be simply :user_return_to for a User class.

like image 71
Austin Taylor Avatar answered Oct 20 '22 00:10

Austin Taylor