插件中的 Rails 2 路由助手
我正在尝试编写一个插件,在我想要执行的任务中,我希望能够从插件中调用路由帮助器方法。例如,如果我有 map.resources :user
,我希望能够从我的插件调用 user_path(:id => 1)
。我不断收到未定义的方法 user_path 错误。
在rails 3中,您可以使用Rails.application.routes.url_helpers来完成此操作,但我似乎无法找到rails 2的替代方案。包括ActionController::UrlWriter 没有帮助。有什么想法吗?
I'm trying to write a plugin, and among the tasks I want to perform I want to be able to call route helper methods from within the plugin. For instance, if I have map.resources :user
, I want to be able to call user_path(:id => 1)
from my plugin. I keep getting undefined method user_path error.
In rails 3, you can do this using Rails.application.routes.url_helpers
, but I don't seem to be able to find an alternative for rails 2. Including ActionController::UrlWriter does not help. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用的是 Rails 2.3.4,我可以在插件的控制器和视图中使用常规路径助手,至少在我测试的插件中是这样。
我无法在插件的 lib 文件中使用它们,但这是因为助手在控制器外部不可用(视图在控制器内部处理,因此它们也可以使用它们)。 lib 文件(插件的核心)往往是加载到模型环境中的模块和类。
您能否提供有关您正在尝试做什么的更多详细信息?
I'm using rails 2.3.4 and i can use my regular path helpers in the controllers and views of my plugins, at least within the ones i tested.
I can't use them in the lib files for the plugins, but that's because the helpers aren't available outside the controllers (the views are dealt with inside the controllers so they can use them too). The lib files (the meat of the plugins) tends to be modules and classes which get loaded into the model environment.
Can you provide more details about what you're trying to do?
您应该能够执行以下操作:
You should be able to do: