gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:37:47 26 4
gpt4 key购买 nike

我有一个模型,其中包含一个名为“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

关于ruby-on-rails - 在 lib 文件夹模块中使用 url_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5875599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com