ruby-on-rails – 在lib文件夹模块中使用url_for

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在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

原文链接:https://www.f2er.com/ruby/273013.html

猜你在找的Ruby相关文章