Rails before_filter和动作识别 [英] Rails before_filter and action identification
问题描述
我想在我的控制器中编写一个before_filter来标识下一步将要执行的动作。
I want to write a before_filter in my controller to identify the action which will execute next. This is for authorization purposes (this is somewhat like role_requirement plugin do..)
Ex:如果用户键入此url,则用于授权目的(这有点类似于role_requirement插件。) :3000 / users ,默认情况下转到用户/索引操作。在我的用户控制器中,我有一个前置过滤器方法,例如 check_permission,我希望该方法将 index作为操作。
Ex: if a user types this url http://localhost:3000/users
, default it goes to users/index action. And in my users controller i'm having a before filter method say 'check_permission' and i want that method to get 'index' as the action.
推荐答案
控制器上的 action_name
方法应该为您提供所需的内容。不过,它没有记录,因此无法保证它有一天不会消失。
The action_name
method on the controller should give you what you're looking for. It's not documented, though, so there is no guarantee it won't disappear someday.
before_filter { |controller| logger.debug "Running before the #{controller.action_name} action" }
这篇关于Rails before_filter和动作识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!