如何从Rails控制器中混合并调用link_to?

发布于 2024-09-25 18:31:59 字数 773 浏览 11 评论 0原文

这似乎是一个菜鸟问题,但简单的答案却让我难以理解。我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绻影浮沉 2024-10-02 18:31:59

与 Rails 3,4 和 5 兼容:

view_context.link_to

Compatible with Rails 3,4 and 5:

view_context.link_to
莫相离 2024-10-02 18:31:59

这并不能真正回答您的问题,但有一种更简单的方法

对于 Rails 5,使用 helpers 代理

helpers.link_to '...', '...'

适用于 Rails 3 和4,由于您使用的是控制器中的帮助器,因此您可以使用 view_context

# in the controller code
view_context.link_to '...', '...'

# instead of using your mixin code 
link_to '...', '...'

对于 Rails 2,因为您使用的是控制器中的帮助器,所以您实际上可以访问以下的 @template 成员变量控制器,@template 是视图,并且已经混合了 UrlHelper

# in the controller code
@template.link_to '...', '...'

# instead of using your mixin code 
link_to '...', '...'

如果您需要从控制器以外的代码使用 urlhelper,您的解决方案可能是要走的路

This doesn't really answer your question but there is an easier way

For Rails 5, use the helpers proxy

helpers.link_to '...', '...'

For Rails 3 and 4, since you are using the helper from a controller you can use the view_context

# in the controller code
view_context.link_to '...', '...'

# instead of using your mixin code 
link_to '...', '...'

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

# in the controller code
@template.link_to '...', '...'

# instead of using your mixin code 
link_to '...', '...'

if you need to use the urlhelper from code other than the controller, your solution is probably the way to go

无可置疑 2024-10-02 18:31:59

我在使用自己的帮助器方法时仍然遇到问题,这些方法在带有 ActionController.helper.my_method 的控制器中使用内置帮助器方法。

显然,对每个闪光灯使用 render_to_string 是可行的,但我不想为每个闪光灯创建这么多小部分。

我的解决方案是为控制器创建一个小助手来部分执行代码。

控制器中的辅助方法:

def h(&block)
  render_to_string(:partial => 'helper', :locals => {:block => block})
end

HAML 部分 (helper.html.haml):

= instance_eval &block 

同样适用于 ERB (helper.html.erb):

<%= instance_eval &block %>

在控制器中像这样使用它来调用辅助程序中定义的 my_custom_helper_function:

redirect_to url, :notice => h{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:

def h(&block)
  render_to_string(:partial => 'helper', :locals => {:block => block})
end

The HAML partial (helper.html.haml):

= instance_eval &block 

Same would work in ERB (helper.html.erb):

<%= instance_eval &block %>

Use it like this in your controller to call the my_custom_helper_function that's defined in a helper:

redirect_to url, :notice => h{my_custom_helper_function}
小嗷兮 2024-10-02 18:31:59

您可以这样做:

ActionController.helpers.link_to("Click Me!", awesome_path)

但实际上,生成该链接的更好位置可能是在帮助程序模块中,其中已经包含了 UrlHelper 和其他与视图相关的帮助程序。

[更新]

这种方法已经过时,不再有效。

You can do it like this:

ActionController.helpers.link_to("Click Me!", awesome_path)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文