How do I use helpers in rake?

后端 未结 2 1722
执笔经年
执笔经年 2021-02-02 05:56

Can I use helper methods in rake?

2条回答
  •  悲&欢浪女
    2021-02-02 06:26

    Yes, you can. You simply need to require the helper file and then include that helper inside your rake file (which actually a helper is a mixin that we can include).

    For example, here I have an application_helper file inside app/helpers directory that contains this:

    module ApplicationHelper
      def hi
        "hi"
      end
    end
    

    so here is my rake file's content:

    require "#{Rails.root}/app/helpers/application_helper"
    include ApplicationHelper
    
    namespace :help do
      task :hi do
        puts hi
      end
    end
    

    and here is the result on my Terminal:

    god:helper-in-rake arie$ rake help:hi 
    hi
    

提交回复
热议问题