在我的应用程序中,控制器之前有代码,它应该在登录后将每种类型的用户重定向到适当的页面。当每种类型的用户注册时,它们应该重定向到根页面,这是相同的退出。当我点击登录或注册按钮时,我会重定向到customer_dashboard而不是登录或注册页面。当我点击退出时,我收到此错误Cannot redirect to nil!
,在添加这些重定向方法之前,当我点击每个按钮时,他们会链接到正确的页面进行登录和注册。这段代码有什么问题?为了使这个网站有效,是否有一些逻辑缺少代码?
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate!
def after_sign_in_path_for(resource)
case resource
when Customer then customer_dashboard_path
when Vendor then stored_location_for(:vendors) || vendor_dashboard_path
end
end
def after_sign_out_path_for(resource)
case resource
when Customer then root_path
when Vendor then stored_location_for(:vendors) || root_path
end
end
def after_inactive_sign_up_path_for(resource)
case resource
when Customer then root_path
when Vendor then stored_location_for(:vendors) || root_path
end
end
def authenticate!
if @current_user == current_customer
:authenticate_customer!
elsif @current_user == current_vendor
:authenticate_vendor!
end
end
end
所以我添加了skip_before_filter:authenticate!所有4个控制器和路由问题仍然存在。
class Vendors::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
skip_before_filter :authenticate!
# GET /resource/sign_up
# def new
# super
# end
# POST /resource
# def create
# super
# end
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
# def update
# super
# end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
答案 0 :(得分:1)
这是因为before_action :authenticate!
因为它在每次操作之前被调用并且您错误地路由了。
我不会想到你的目标是什么,但在进行过滤之前必须先跳过这个,然后再进行登录或注册等操作。