我有一种方法可以执行以下操作:
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
发布评论