2012-07-09 60 views
0

如何在视图图层中提供以下方法?公开控制器的类方法来查看辅助方法?

# app/controllers/base_jobs_controller.rb 
class BaseJobsController < ApplicationController 
    def self.filter_name 
    (controller_name.singularize + "_filter").to_sym 
    end 
end 

我想在这样的视图助手来使用它:

module ApplicationHelper 
    def filter_link(text, options = {}) 
    # Need access to filter_name in here.... 
    end 
end 

回答

2

相反的helper_method,我宁愿包括模块中这样的功能。

module BaseJobsHelp 
    def filter_name 
    (controller_name.singularize + "_filter").to_sym 
    end 
end 

然后包括在BaseJobsControllerApplicationHelper模块。

class BaseJobsController < ApplicationController 
    include BaseJobsHelp 

    # ... 
end 


module ApplicationHelper 
    include BaseJobsHelp 

    def filter_link(text, options = {}) 
    # You can access filter_name without trouble 
    end 
end 

取决于你的模块的方法的内容,您可能需要使用另一种方法来访问某些数据(即电流控制器的名字)