如何从Rails控制器中混合并调用link_to?
这似乎是一个菜鸟问题,但简单的答案却让我难以理解。我需要在 ActionController
方法中调用 link_to
来吐出 HTML 链接。 ActionView::Helpers::UrlHelper.link_to
调用 url_for
,但这会调用 AV 模块的版本而不是控制器的版本。 来强制它执行我想要的操作
#FIXME there must be a better way to mixin link_to
alias_method :self_url_for, :url_for
include ActionView::Helpers::UrlHelper
alias_method :url_for, :self_url_for
我设法通过放入控制器 。但是,我仍然不确定它为什么有效。有人可以解释一下这里发生的方法范围和隐藏吗?有什么更好的方法来混合 link_to
(或者一般来说,只包含模块中的一些方法),以便我可以在控制器中调用它(用例是生成带有链接的 Flash 字符串。)
请不要进行有关 MVC 的讲座 - 如果有的话,link_to
应该位于与 url_for
分开的模块中。从这方面的噪音来看,很多人都遇到了这个看似微不足道的障碍,最终浪费了一个小时以“Rails 方式”完成它,而真正需要的是一分钟的破解来让我的应用程序现在运行。是否有一种“Rails 方式”可以通过助手来完成此操作?或者更好的红宝石方式?
This seems like a noob question, but the simple answer is eluding me. I need to call link_to
in an ActionController
method to spit out an HTML link. ActionView::Helpers::UrlHelper.link_to
calls url_for
, but this calls the AV module's version instead of the controller's. I managed to coerce this into doing what I intended by putting
#FIXME there must be a better way to mixin link_to
alias_method :self_url_for, :url_for
include ActionView::Helpers::UrlHelper
alias_method :url_for, :self_url_for
in the controller. But, I'm still not sure why it works exactly. Could someone please explain the method scope and hiding that's happening here? What's a better way to mix in link_to
(or generally, to include only some methods from a module) so I can call it in the controller (generating a flash string with a link is the use case.)
Please, no lectures about MVC--if anything, link_to
should be in a module separate from url_for
. Judging from the amount of noise on this, lots of people run into this seemingly trivial snag and end up wasting an hour doing it the "Rails way" when really what is wanted is a one minute hack to make my app work now. Is there a "Rails way" to do this with helpers perhaps? Or a better ruby way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与 Rails 3,4 和 5 兼容:
Compatible with Rails 3,4 and 5:
这并不能真正回答您的问题,但有一种更简单的方法
对于 Rails 5,使用
helpers
代理适用于 Rails 3 和4,由于您使用的是控制器中的帮助器,因此您可以使用 view_context
对于 Rails 2,因为您使用的是控制器中的帮助器,所以您实际上可以访问以下的 @template 成员变量控制器,@template 是视图,并且已经混合了 UrlHelper
如果您需要从控制器以外的代码使用 urlhelper,您的解决方案可能是要走的路
This doesn't really answer your question but there is an easier way
For Rails 5, use the
helpers
proxyFor Rails 3 and 4, since you are using the helper from a controller you can use the view_context
For Rails 2, since you are using the helper from a controller you can actually access the @template member variable of the controller, the @template is the view and already has the UrlHelper mixed in
if you need to use the urlhelper from code other than the controller, your solution is probably the way to go
我在使用自己的帮助器方法时仍然遇到问题,这些方法在带有 ActionController.helper.my_method 的控制器中使用内置帮助器方法。
显然,对每个闪光灯使用 render_to_string 是可行的,但我不想为每个闪光灯创建这么多小部分。
我的解决方案是为控制器创建一个小助手来部分执行代码。
控制器中的辅助方法:
HAML 部分 (helper.html.haml):
同样适用于 ERB (helper.html.erb):
在控制器中像这样使用它来调用辅助程序中定义的 my_custom_helper_function:
I still had problems using my own helper methods that use built-in helper methods in a controller with ActionController.helper.my_method.
Obviously using render_to_string for each flash would work, but I don't want to create so many small partials for each flash.
My solution was to create a little helper for controllers to execute code in a partial.
The helper method in the controller:
The HAML partial (helper.html.haml):
Same would work in ERB (helper.html.erb):
Use it like this in your controller to call the my_custom_helper_function that's defined in a helper:
您可以这样做:
但实际上,生成该链接的更好位置可能是在帮助程序模块中,其中已经包含了 UrlHelper 和其他与视图相关的帮助程序。
[更新]
这种方法已经过时,不再有效。
You can do it like this:
But really, a better place to generate that link might be in a helper module where UrlHelper and other view-related helpers are already included.
[update]
This approach is outdated and no longer works.