如何在Rails中动态调用路由助手? [英] How to dynamically call routes helper in rails?
问题描述
例如,我构造了一个名为 new_work_path的字符串,现在我想将该助手称为方法。
For example, I have constructed a string called "new_work_path", now I want to call that helper as a method.
我尝试了 send( new_work_path,vars)
并从许多对象调用相同的发送。但是我认为我找不到合适的对象来调用这些助手。
I've tried send("new_work_path", vars)
and calling the same send from many objects. But I don't think that I've found the right object to call these helpers.
要做 object.send( new_work_path, vars)
,我应该寻找什么对象?
To do object.send("new_work_path", vars)
, what object should I be looking for?
我试图在线寻找该对象一段时间,但找不到任何东西。如果有人可以照亮它,那就太好了!
I've tried to look for this online for a while but couldn't find anything. If anyone can shine some lights on this one, it would be great!
谢谢!
推荐答案
尝试 Rails.application.routes.url_helpers.send(...)
编辑:
作为Larry Gebhardt 提到 url_helpers
模块不再被缓存。
As Larry Gebhardt mentioned the url_helpers
module is no longer being cached.
另一种解决方法是:
cached_helpers = Class.new do
include Rails.application.routes.url_helpers
include Rails.application.routes.mounted_helpers
end.new
cached_helpers.send(...)
这篇关于如何在Rails中动态调用路由助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!