Cookie检测 - 如何修复过滤器链停止为:cookies_required呈现或重定向?

时间:2016-06-14 21:55:50

标签: ruby-on-rails cookies

我正在使用ruby" 2.3.1"和" rails"," 5.0.0.rc1"另外,使用Devise ....

我想检测Cookies&在此基础上,提供一条简单的消息说“用户必须启用cookie”#39;

这是我的代码:

路线:

match "cookie_test" => "application#cookie_test", as: "cookies_test", via: :all

application_controller

require "activerecord/session_store"
require "cookie_detection"
class ApplicationController < ActionController::Base

  include CookieDetection
  include PublicActivity::StoreController
  include SuperadminHelper
  include GoogleHelper ....

LIB / cookie_detection.rb

module CookieDetection

  def self.included(base)
    base.before_filter :cookies_required, :except => ["cookie_test"]
  end

  # checks for presence of "cookie_test" cookie
  # (should have been set by cookies_required before_filter)
  # if cookie is present, continue normal operation
  # otherwise show cookie warning at "shared/cookies_required"

  def cookie_test
    Util.log " COOKIE TEST METHOD"
    if cookies["cookie_test"].blank?
      Util.log " COOKIE TEST IS BLANK"
      logger.warn("=== cookies are disabled")
      render "shared/cookies_required", layout: "header-only"
    else
      Util.log "I AM IN ELSE OF COOKIE TEST"
      redirect_back_or_default(dashboard_path)
    end
  end

  protected

  # checks for presence of "cookie_test" cookie.
  # If not present, redirects to cookies_test action
  def cookies_required
    Util.log " COOKIE REQUIRED METHOD"
    return true unless cookies["cookie_test"].blank?
    cookies["cookie_test"] = Time.now
    session[:return_to] = request.url
    redirect_to(cookies_test_path)
  end

end

cookies_required.html.erb

<h2>Cookies must be enabled to use AgentBright.</h2>

似乎陷入了一个循环中。超时:

GET     "/users/sign_in"  for 1  at 2016-06-14 17:17:16 -0400
[INFO ] === Processing by Users::SessionsController#new as HTML
[INFO ] Redirected to http://localhost:3000/cookie_test
[INFO ] Filter chain halted as :cookies_required rendered or redirected
[INFO ] Completed 302 Found in 2ms (ActiveRecord: 0.0ms)
[INFO ] source=rack-timeout id=ecc0d84372c0fcfffb1d3acea378be85 timeout=60000ms service=38ms state=completed
[INFO ] source=rack-timeout id=603383b900f361856e17d424454bb481 timeout=60000ms state=ready
[DEBUG] source=rack-timeout id=603383b900f361856e17d424454bb481 timeout=60000ms service=0ms state=active
[INFO ] GET     "/cookie_test"  for 1  at 2016-06-14 17:17:16 -0400
[INFO ] === Processing by ApplicationController#cookie_test as HTML
[INFO ] Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)
[INFO ] source=rack-timeout id=603383b900f361856e17d424454bb481 timeout=60000ms service=32ms state=completed
[INFO ] source=rack-timeout id=532de77ff9798fa2b3f37d1c1975d802 timeout=60000ms state=ready
[DEBUG] source=rack-timeout id=532de77ff9798fa2b3f37d1c1975d802 timeout=60000ms service=0ms state=active
[INFO ] GET     "/users/sign_in"  for 1  at 2016-06-14 17:17:16 -0400

如何获取共享/ cookies_required.html.erb进行渲染?有任何想法吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

Completed 401 Unauthorized

查看其身份验证问题。你在使用Devise吗?如果是这样,请在application_controller.rb中更改此内容:

before_action :authenticate_user!

到此:

before_action :authenticate_user!, except: :cookie_test