Ruby on rails 3 如何使用Desive中的会话[:user.return_to]

Ruby on rails 3 如何使用Desive中的会话[:user.return_to],ruby-on-rails-3,session,login,devise,Ruby On Rails 3,Session,Login,Devise,我正在尝试使用会话[:user.return\u to],但没有成功 我的代码: def after_sign_in_path_for(resource) (session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s end 所以,问题是:当我检查会话变量时,我没有任何与这些名称相关的变量 我想在被重定向到登录页面之前进入我正在处理的页面 在重定向某人之前,需要定义会话变量 session[:url_ba

我正在尝试使用
会话[:user.return\u to]
,但没有成功

我的代码:

def after_sign_in_path_for(resource)
  (session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s
end
所以,问题是:当我检查会话变量时,我没有任何与这些名称相关的变量


我想在被重定向到登录页面之前进入我正在处理的页面

在重定向某人之前,需要定义会话变量

session[:url_back] = "/***

与同样的问题作斗争。到目前为止,我有一个大致相同的解决方案

在我的应用程序中_controller.rb

  before_filter :set_page # at the top and then

  protected
  def set_page
    unless request.referer.include?('/users/sign')
      session[:return_to] = request.referer
    end
  end
 def after_sign_in_path_for(resource)
   sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')
   if request.referer == sign_in_url
     super
   else
     stored_location_for(resource) || request.referer || root_path
   end
 end

除非语句用于防止用户返回到登录或注销页面。我不喜欢url是硬编码在这里,所以我想看到一个更好的解决方案。另外,在重定向到登录页面的页面上似乎从未发生过before_filter,因此如果您单击需要登录的内容,登录后您将返回到单击链接的页面,而不是实际链接本身。

这可能会有所帮助

使用新版本的Deviate(我在2.1.2上),在过滤器之前添加
:authenticate\u user到控制器将自动为您设置此行为

如果仍要手动设置,一种方法是在
会话中设置路径。Deviate希望在
会话[“{resource}\u return\u to”]
中找到它,其中'resource'是您的用户模型的名称。所以通常这是
会话[“用户返回”]
。您的版本中有句点“.”,这可能是导致错误的原因。另外,
会话
接受键的字符串值,因此无需尝试对其进行符号化

另一种方法是在对(资源)
方法的路径进行签名后覆盖
。在Desive wiki上有关于如何执行此操作的更多详细信息

最后,由于这将发出重定向,您可能需要考虑使用完整URL。

def after_sign_in_path_for(resource)
  session["#{resource}_return_to"] || root_url
end

在您的ApplicationController.rb中

  before_filter :set_page # at the top and then

  protected
  def set_page
    unless request.referer.include?('/users/sign')
      session[:return_to] = request.referer
    end
  end
 def after_sign_in_path_for(resource)
   sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')
   if request.referer == sign_in_url
     super
   else
     stored_location_for(resource) || request.referer || root_path
   end
 end

(资源)的存储位置是Desive的内置方法,用于存储用户来自的位置。

使用
会话[:“user.return\u to”]
的具体原因是什么<代码>会话[:return_to]
是设备文档中记录的会话。该会话可能重复