[英]detect controller type in module ruby
I created a module, basically what I want to do is, 我创建了一个模块,基本上我想做的是
in this module, there is a function that will work like before_filter. 在此模块中,有一个功能将类似于before_filter。 This function will perform the logic and determine what it should perform.
此功能将执行逻辑并确定应执行的操作。 Example
例
class JobsController < ApplicationController
include Mymodule
authorize_resources
def create
end
def update
end
end
module Mymodule
def authorize_resources
current_controller = params[:controller]
if current_controller == 'jobs'
//some logic
end
end
end
so how I actually can automatically detect the controller name based on where my function located such as jobs, users, and etc. This is something similar to CanCan, but I would like to make my own. 因此我实际上是如何根据功能所在的位置(例如作业,用户等)自动检测控制器名称的。这类似于CanCan,但我想自己做。
Besides, how can I raise an exception or redirect_to a path if it failed, is that need to extend some rails classes? 此外,如果失败,如何引发异常或redirect_to路径,是否需要扩展一些rails类?
def authorize_resources
if current_controller.class == 'jobs'
//logic
end
end
Change your if
to: 将您的
if
更改为:
if(current_controller == JobsController)
If params[:controller]
is the class itself, and 如果
params[:controller]
是类本身,并且
if(current_controller.class == JobsController)
If the variable is an instance of JobsController
. 如果变量是
JobsController
的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.