重定向导轨后会话值重置4 [英] Session Values getting reset after redirect rails 4

查看:152
本文介绍了重定向导轨后会话值重置4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devise 3.5.1,rails 4.0.0和ruby 2.0.0-p0。



如果用户没有登录并尝试打开私人页面需要认证,让我们说 lvh.me:3000/users/1/edit ,然后设计将添加 users / 1 / edit 在会话中的密钥 user_return_to 。问题是当用户在身份验证失败后被重定向到登录页面时,会话中的 user_return_to 值将被重置。



我的应用程序有这个问题。如果我在会话中存储一个值,并从相同的操作重定向到另一个路径,那么会话值将消失。



注意: (rails 3.0.4,ruby 1.8.7)到(rails 4.0.0,ruby 2.0.0)的rails应用程序。



对不起,任何歧义的问题。如果您有任何问题请发表评论。



更新:



应用程序控制器,我添加了一个以前的过滤器来验证用户。



application_controller.rb

  class ApplicationController< ActionController :: Base 
protect_from_forgery

before_filter:authenticate_user !,:unless => :devise_controller?

#其余的代码

end

我正在使用一个继承 Devise :: FailureApp 的自定义失败类。代码如下所示。



custom_failure.rb

 code> class CustomFailure< Devise :: FailureApp 
def redirect_url
evalnew_user_session_path
end

def redirect
store_location!
flash [:alert] = i18n_message
#session [:user_return_to]这里返回'/ foobar'。
redirect_to redirect_url
end

end

我已经在上面的代码中注释了一行。会话[:user_return_to]在该行可用,但在下一行重定向之后,会话[:user_return_to]变为无。



我已将设备升级到1.5 .3到3.5.1。

解决方案

我在这里添加解决方案,以防任何人遇到同样的问题。 p>

我的应用程序中有一个名为asset的资源,与资产管道冲突。当我的网址包含资产时,会话被重置。 '/资产'。因此,如果您有一个名为资产的资源,那么您必须更改资产管道的前缀。为此,请在您的 application.rb 文件中添加以下行。



config.assets.prefix =' / static_assets'



此外,您必须将资产管道文件夹重命名为 static_assets



请注意,此解决方案仅适用于您的应用程序中名为资源的资源, rails版本是3.1 +


I am using devise 3.5.1, rails 4.0.0 and ruby 2.0.0-p0.

If a user is not signed in and tries to open a private page which requires authentication, let's say lvh.me:3000/users/1/edit, then devise will add users/1/edit in session against the key user_return_to. The problem is that when the user gets redirected to the login page after authentication failure then user_return_to value in session gets reset.

I have this problem all over my app. If I store a value in session and redirect to another path from the same action then the session value disappears.

Note: I have upgraded my rails app from (rails 3.0.4, ruby 1.8.7) to (rails 4.0.0, ruby 2.0.0).

Sorry for any ambiguities in the question. If you have any question please comment it.

UPDATE:

In my application controller, I've added a before filter to authenticate user. Only relevant code is shown in application controller.

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :authenticate_user!, :unless => :devise_controller?

  # rest of the code

end

I'm using a custom failure class which inherits Devise::FailureApp. The code looks like this.

custom_failure.rb

class CustomFailure < Devise::FailureApp
  def redirect_url
    eval "new_user_session_path"
  end

  def redirect
    store_location!
    flash[:alert] = i18n_message
    # session[:user_return_to] returns '/foobar' here.
    redirect_to redirect_url
  end

end

I have commented a line in above code. session[:user_return_to] is available at that line but after the redirect on very next line, the session[:user_return_to] becomes nil.

I've upgraded devise from 1.5.3 to 3.5.1.

解决方案

I'm adding the solution here in case anybody else encounters the same problem.

I had a resource named asset in my application and it was conflicting with the asset pipeline. The session was getting reset whenever my URL contained 'assets' e.g. '/assets'. So if you have a resource named asset then you have to change the prefix of your asset pipeline. To do so, add the following line in your application.rb file.

config.assets.prefix = '/static_assets'

Also you have to rename the asset pipeline folder to static_assets.

Please note that this solution only applies if you have a resource named Asset in your application and the rails version is 3.1+

这篇关于重定向导轨后会话值重置4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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