简体   繁体   中英

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?

我相信会话密钥是:"#{scope}_return_to ,它将简单地为:user_return_to用于User类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM