在lib文件夹模块中使用url_for

我有一个名为“type”的字段和一个名为“value”的字段的模型。 类型字段将确定在呈现之前如何解析“值”。 我希望这很容易扩展,所以我在我的lib文件夹中放了各种“格式化程序”类。

我的一个“格式化程序”调用了url_for:

class CustomTypeFormatter include ActionView::Helpers::TextHelper include ActionView::Helpers def show raw sanitize( auto_link( value ) ) end def get_url(page) url_for( :controller => :my_controller, :action => :show, :path => page.path ) end end 

问题是,url_for引发了这个错误:

 undefined local variable or method `_routes' 

我想我只是错过了一个包含。 有谁知道它应该是什么?

**更新* * *

这是堆栈跟踪的一部分:

 actionpack (3.0.7) lib/action_dispatch/routing/url_for.rb:131:in `url_for' actionpack (3.0.7) lib/action_view/helpers/url_helper.rb:99:in `url_for' 

所以我认为它必须是一些ActiveDispatch依赖,虽然我无法弄清楚

胜利!

 include ActionView::Helpers include ActionDispatch::Routing include Rails.application.routes.url_helpers 

通过http://apidock.com/rails/ActionController/UrlWriter