我想打电话多态路径 http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html#method-i-polymorphic_path在位于的帮助模块中lib/my_module.rb
.
我尝试了以下来自这个答案 https://stackoverflow.com/questions/27003252/how-can-i-access-polymorphic-path-inside-a-model-in-rails-4,它适用于模型,但不适用于我的模块:
module MyModule
include ActionDispatch::Routing::PolymorphicRoutes
include Rails.application.routes.url_helpers
def link(model)
polymorphic_path(model)
end
end
I get:
undefined method `polymorphic_path' for MyModule:Module
顺便说一句,我通过加载我的模块config.autoload_paths += %W(#{config.root}/lib)
in config/application.rb
.
结果你必须创建一个类才能正确包含 ruby 中的内容,例如:
class MyClass
include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers
def link(model)
polymorphic_path(model)
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)