0

How do I make the following method available in the view layer?

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

I want to use it in a view helper like this:

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

1 回答 1

2

而不是helper_method,我更喜欢在模块中包含这样的功能。

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

然后将模块包含在BaseJobsControllerandApplicationHelper中。

class BaseJobsController < ApplicationController
  include BaseJobsHelp

  # ...
end


module ApplicationHelper
  include BaseJobsHelp

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

根据模块中方法的内容,您可能需要使用替代方法来访问某些数据(即当前控制器的名称)

于 2012-07-09T20:53:40.283 回答