Ruby-on-rails 如何从 Rails 中的控制器混合和调用 link_to?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3843509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:18:24  来源:igfitidea点击:

How to mixin and call link_to from controller in Rails?

ruby-on-railshelpersactionviewlink-tomixins

提问by tribalvibes

This seems like a noob question, but the simple answer is eluding me. I need to call link_toin an ActionControllermethod to spit out an HTML link. ActionView::Helpers::UrlHelper.link_tocalls 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

这似乎是一个菜鸟问题,但我无法找到简单的答案。我需要调用link_to一个ActionController方法来吐出一个 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

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.)

在控制器中。但是,我仍然不确定为什么它完全有效。有人可以解释方法范围并隐藏这里发生的事情吗?有什么更好的混合方式link_to(或者通常只包含模块中的一些方法)以便我可以在控制器中调用它(生成带有链接的 flash 字符串是用例。)

Please, no lectures about MVC--if anything, link_toshould 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?

请不要讲关于 MVC 的讲座——如果有的话,link_to应该放在一个独立于url_for. 从这方面的噪音量来看,很多人遇到了这个看似微不足道的障碍,最终浪费了一个小时以“Rails 方式”来做,而真正需要的是一分钟的 hack 以使我的应用程序现在可以工作。有没有一种“Rails 方式”可以用助手来做到这一点?或者更好的红宝石方式?

回答by Yacine Zalouani

Compatible with Rails 3,4 and 5:

与 Rails 3,4 和 5 兼容:

view_context.link_to

回答by house9

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

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

For Rails 5, use the helpersproxy

对于 Rails 5,使用helpers代理

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

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

对于 Rails 3 和 4,由于您使用的是控制器中的帮助程序,因此您可以使用 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

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

# 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

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

回答by brentmc79

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.

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

[update]

[更新]

This approach is outdated and no longer works.

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

回答by Sascha Konietzke

I still had problems using my own helper methods that use built-in helper methods in a controller with ActionController.helper.my_method.

在使用 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.

显然,为每个闪光灯使用 render_to_string 会起作用,但我不想为每个闪光灯创建这么多小部分。

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):

HAML 部分 (helper.html.haml):

= instance_eval &block 

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

同样适用于 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:

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

redirect_to url, :notice => h{my_custom_helper_function}