before_filter 带参数 [英] before_filter with parameters
问题描述
我有一个方法可以做这样的事情:
I have a method that does something like this:
before_filter :authenticate_rights, :only => [:show]
def authenticate_rights
project = Project.find(params[:id])
redirect_to signin_path unless project.hidden
end
我也想在其他一些控制器中使用这个方法,所以我把这个方法复制到了一个包含在 application_controller 中的 helper.
I also want to use this method in some other Controllers, so i copied the method to a helper that is included in the application_controller.
问题是,在某些控制器中,项目的 id 不是 :id
符号而是 f.e.:project_id
(还有一个 :id
存在(对于另一个模型)
the problem is, that in some controllers, the id for the project isn't the :id
symbol but f.e. :project_id
(and also a :id
is present (for another model)
你会如何解决这个问题?是否可以选择向 before_filter 操作添加参数(传递正确的参数)?
How would you solve this problem? is there an option to add a parameter to the before_filter action (to pass the right param)?
推荐答案
我会这样做:
before_filter { |c| c.authenticate_rights correct_id_here }
def authenticate_rights(project_id)
project = Project.find(project_id)
redirect_to signin_path unless project.hidden
end
其中 correct_id_here
是访问 Project
的相关 ID.
Where correct_id_here
is the relevant id to access a Project
.
这篇关于before_filter 带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!