我一直在努力将用户(登录后)重定向到之前的网址。但是我收到了以下错误:
Filter chain halted as :require_no_authentication rendered or redirected
所以我在网上看到它,我看到在会话控制器中预先设置过滤器可以解决问题:
class SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, only: [ :new, :create ]
end
但我仍然遇到同样的错误。有没有其他方法可以解决这个问题?
应用程序控制器
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
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
由于
答案 0 :(得分:1)
设计会话控制器已为:require_no_authentication
和:new
添加了:create
。在我的代码中,我有一个save_last
方法,就像你的store_location一样,在application_controller.rb
中定义为before_action
。
答案 1 :(得分:1)
除非您需要覆盖Devise的控制器,否则无需使用它们,但如果您确实需要,请删除prepend_before_filter :require_no_authentication
。
这是我用于重定向到上一个网址的内容:
# app/controllers/application_controller.rb
after_action :store_location
# If you are requiring authentication for all your controllers you can use
# before_action :authenticate_user! unless: :devise_controller?
# and disable particular actions in other controllers with
# require_no_authentication (except on any DeviseController)
private
def after_sign_in_path_for(resource)
stored_location_for(resource) || dashboard_path
end
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
并且
# config/routes.rb
devise_for :user
authenticated :user do
root to: 'sistema/inicio#tablero_de_mando', as: :authenticated_user_root
end
root to: 'some_controller#some_action'
您可以在控制器上使用authenticate_user!
或require_no_authentication
和before_action / before_filter,具体取决于天气,或者您不需要对该控制器的特定操作进行身份验证。