문제

I have a custom module I would like to access in my mail helper, but i can't figure out how to include it.

My custom module lib/workday.rb:

module Workday
  def next_workday(date = Date.today)
    ...
  end
  ...
end

That i try to use in my MailHelper:

module MailHelper
  include Workday

  def next_workday(date = Date.today)
    Workday.next_workday(date)
  end
  ...
end

When trying to use the helper i get this:

undefined method `next_workday' for Workday:Module
/www/xxx/app/helpers/mail_helper.rb:4:in `next_workday'

When manually including the module in the console it works fine directly and through the helper:

> include Workday
=> Object
> Workday.next_workday
=> Fri, 04 Jan 2013 
> helpers.next_workday
=> Fri, 04 Jan 2013
도움이 되었습니까?

해결책

If you include a Module the methods will be accessible as instance methods. If you try to access the next_workday method directly through the module, the method must be defined as a "class method" (with self.).

Your MailHelper should work if you use:

module MailHelper
  include Workday
end

There is no need to define the next_workday method in MailHelper since it just delegates to Workday#next_workday anyways. If you include your MailHelper into a class you can then access the method with next_workday

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top