在lib文件夹模块中使用url_for

ear*_*old 13 ruby-on-rails ruby-on-rails-3

我有一个名为"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
Run Code Online (Sandbox Code Playgroud)

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

undefined local variable or method `_routes'
Run Code Online (Sandbox Code Playgroud)

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

**更新***

这是堆栈跟踪的一部分:

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'
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*ich 17

胜利!

include ActionView::Helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)

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