简体   繁体   English

如何在Rails中混合并调用link_to来自控制器?

[英]How to mixin and call link_to from controller in 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. 我需要在ActionController方法中调用link_to来吐出HTML链接。 ActionView::Helpers::UrlHelper.link_to calls url_for , but this calls the AV module's version instead of the controller's. ActionView::Helpers::UrlHelper.link_to调用url_for ,但这会调用AV模块的版本而不是控制器的版本。 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.) 什么是更好的方式混合link_to (或通常,只包括模块中的一些方法)所以我可以在控制器中调用它(生成带有链接的flash字符串是用例。)

Please, no lectures about MVC--if anything, link_to should be in a module separate from url_for . 请不要关于MVC的讲座 - 如果有的话, link_to应该在与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. 从噪音的数量来看,很多人遇到这个看似微不足道的障碍,并最终浪费一个小时做“Rails方式”,真正想要的是一分钟黑客让我的应用程序现在工作。 Is there a "Rails way" to do this with helpers perhaps? 是否有“Rails方式”可能与助手一起做这件事? Or a better ruby way? 还是更好的红宝石方式?

与Rails 3,4和5兼容:

view_context.link_to

This doesn't really answer your question but there is an easier way 这并没有真正回答你的问题,但有一种更简单的方法

For Rails 5 , use the helpers proxy 对于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,那么您的解决方案可能就是您的选择

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. 这种方法已经过时,不再有效。

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM