设计after_sign_in_path错误:过滤链暂停为:require_no_authentication

时间:2014-05-12 21:57:46

标签: ruby-on-rails devise

我正在关注Devise wiki,以便在登录后将用户重定向到当前页面(https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

但是我收到以下错误:

Started GET "/login" for 127.0.0.1 at 2014-05-12 18:52:52 -0300
Processing by Devise::SessionsController#new as HTML
  ←[1m←[35mUser Load (3.0ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://localhost:3000/login
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 8ms (ActiveRecord: 3.0ms)

这是我在应用程序控制器上的代码:

  after_filter :store_location

  def store_location
    # store last url - this is needed for post-login redirect to whatever the user last visited.
    if (request.fullpath != "/users/sign_in" &&
        request.fullpath != "/users/sign_up" &&
        request.fullpath != "/users/password" &&
        !request.fullpath.match("/users") &&
        !request.xhr? && # don't store ajax calls
        request.format == "text/html" || request.content_type == "text/html")
      session[:previous_url] = request.fullpath 
      session[:last_request_time] = Time.now.utc.to_i
    end
  end

  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

知道为什么会这样吗?感谢

的routes.rb

Myapp::Application.routes.draw do
  ActiveAdmin.routes(self)
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations" }, :skip => [:sessions] 
  as :user do
    get 'login' => 'devise/sessions#new', :as => :new_user_session
    post 'login' => 'devise/sessions#create', :as => :user_session
    delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
  end

  root  'static_pages#home'

devise.rb

Devise.setup do |config|
  config.secret_key = ENV['DEVISE_SECRET_KEY']

  require 'devise/orm/active_record'

  # omniauth login
  require "omniauth-facebook"
  config.omniauth :facebook, ENV["APP_ID"], ENV["APP_SECRET"],
      {:scope => 'email, offline_access, user_events, friends_events', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}} 

  # add username to login
  config.authentication_keys = [ :login ]
  config.reset_password_keys = [ :email ]
  config.confirmation_keys = [ :login ]

  config.case_insensitive_keys = [ :email ]

  config.strip_whitespace_keys = [ :email ]

  config.skip_session_storage = [:http_auth]

  config.stretches = Rails.env.test? ? 1 : 10

  config.reconfirmable = false

  config.password_length = 8..128

  config.reset_password_within = 6.hours

  config.sign_out_via = :delete
end

1 个答案:

答案 0 :(得分:0)

您的问题是您的会话[:previous_url]存储完整路径和完整路径是/之后的所有内容,这意味着您尝试重定向到/用户,例如,为什么您收到错误而您的其他条件将永远不会执行,因为全路径永远不会是零。

尝试存储在您的会话[:previous_url] "#{request.protocol}#{request.host_with_port}#{request.fullpath}"request.original_url 它肯定会解决你的问题。