Rails在配置文件中使用自定义帮助程序

时间:2014-05-21 08:53:36

标签: ruby-on-rails

我有一个自定义助手:

module PostsHelper
    def post_path(post)
        year = post.published_at.year

        month = post.published_at.month
        month = "0#{ month }" if month < 10

        day = post.published_at.day
        day = "0#{ day }" if day < 10

        "#{ blog_path }/#{ year }/#{ month }/#{ day }/#{ post.raw_title }"
    end
end

我想在我用来构建站点地图的config / sitemap.rb文件中使用帮助器。

我在文件中尝试这个:

Post.posts.each do |bp|
    add PostsHelper::post_path(bp), priority: 1, changefreq: 'weekly'
end

但我一直收到这个错误:

NoMethodError: undefined method `post_path' for PostsHelper:Module

尝试包含该模块也无济于事(include PostsHelper):

NoMethodError: undefined method `include' for #<SitemapGenerator::Interpreter:0x000000025086b8> 

如何在配置文件中使用自定义Rails助手?

1 个答案:

答案 0 :(得分:4)

您必须在config/sitemap.rb

中加入帮助程序模块

将其添加到config/sitemap.rb文件的顶部

SitemapGenerator::Interpreter.send :include, PostsHelper
相关问题