带有参数的before

编程入门 行业动态 更新时间:2024-10-09 21:29:00
本文介绍了带有参数的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

Where correct_id_here is the relevant id to access a Project.

更多推荐

带有参数的before

本文发布于:2023-10-30 07:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!