使用设计手动设置 return_to [英] Manually setting return_to with devise

查看:12
本文介绍了使用设计手动设置 return_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 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:

查看:

<p id="save"><%= link_to "Save", new_save_path, :remote => true %></p>

saves_controller.rb:

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

如您所见,由于操作是 ajax 操作,我无法使用传统的 before_filter :authenticate_user! 方法.因此,我将用户重定向到登录页面.

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.

我知道我可以用 session[:"user.return_to"] 做到这一点,但我在设置它时遇到了问题.我怎样才能做到这一点?还是我的做法全错了?

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

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

这篇关于使用设计手动设置 return_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆