Ruby on rails 在\u过滤器要求之前\u登录创建一个无限循环
我是ruby和Rails的新手。我正在我的应用程序中使用Desive和CanCan以及Rails_Admin。 我正在尝试->如果一个用户已经登录,并且是管理员,重定向到rails\u admin\u路径如果它不是管理员但只是一个用户,那么重定向到“upload\u path”,如果它没有登录,那么重定向到sign\u in路径,但是可能由于我缺乏知识,我正在创建一个无限重定向循环。即使我尝试在没有“要求登录”筛选器的情况下访问登录 以下是我迄今为止所做的工作: 应用程序\u controller.rbRuby on rails 在\u过滤器要求之前\u登录创建一个无限循环,ruby-on-rails,ruby,devise,cancan,rails-admin,Ruby On Rails,Ruby,Devise,Cancan,Rails Admin,我是ruby和Rails的新手。我正在我的应用程序中使用Desive和CanCan以及Rails_Admin。 我正在尝试->如果一个用户已经登录,并且是管理员,重定向到rails\u admin\u路径如果它不是管理员但只是一个用户,那么重定向到“upload\u path”,如果它没有登录,那么重定向到sign\u in路径,但是可能由于我缺乏知识,我正在创建一个无限重定向循环。即使我尝试在没有“要求登录”筛选器的情况下访问登录 以下是我迄今为止所做的工作: 应用程序\u controlle
class ApplicationController < ActionController::Base
before_filter :require_login
protect_from_forgery
#IDEA 1
#def require_login
# if current_user.role? == 'Administrator'
# redirect_to rails_admin_path
# elsif current_user.role? == (('C1' or 'D1') or ('M1' or 'M2'))
# redirect_to upload_path
# end
# end
#I saw this somewhere and It doesn't work either
def require_login
redirect_to new_user_session_path, alert: "You must be logged in to perform this action" if current_user.nil?
end
rescue_from CanCan::AccessDenied do |e|
redirect_to new_user_session_path, alert: e.message
end
end
能力.rb
class Ability
include CanCan::Ability
def initialize(user)
#Define abilities for the passed in user here.
user ||= User.new #guest user (not logged in)
#a signed-in user can do everything
if user.role == 'Administrator'
#an admin can do everything
can :manage, :all
can :access, :rails_admin # grant access to rails_admin
can :dashboard # grant access to the dashboard
elsif user.role == (('C1' or 'M1') or ('D1' or 'M1'))
# can :manage, [ProductList, Inventory]
# can :read, SiteConfigurationList
# end
end
end
当我运行rake路由时,我得到designe和Rails_admin路由,以及“upload”路由。
我真的试图纠正这个愚蠢的错误,但老实说,我已经没有办法了。如果您能为我提供任何帮助,我将不胜感激。先谢谢你 问题在于,您的应用程序控制器中有一个要求用户登录的
前置过滤器。基本上,您要求用户在访问登录页面之前登录
您可以使用Desive的内置方法:authenticate\u user
:
before_filter :authenticate_user!
或者,您可以指定您的before\u过滤器
不在来自DevisionController的操作上运行
before_filter :require_login, :unless => :devise_controller?
问题在于,您的ApplicationController中有一个要求用户登录的前置过滤器。基本上,您要求用户在访问登录页面之前登录
您可以使用Desive的内置方法:authenticate\u user
:
before_filter :authenticate_user!
或者,您可以指定您的before\u过滤器
不在来自DevisionController的操作上运行
before_filter :require_login, :unless => :devise_controller?