带有参数的before_filter [英] before_filter with parameters

查看:152
本文介绍了带有参数的before_filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以执行以下操作:

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

我也想在其他一些Controller中使用此方法,因此我将该方法复制到了application_controller中包含的一个助手中

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 符号但是fe :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 是访问项目的相关ID code>。

Where correct_id_here is the relevant id to access a Project.

这篇关于带有参数的before_filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆