How to mix and call link_to from a controller in Rails?

It sounds like a noob question, but a simple answer eludes me. I need to call link_to in the link_to method to push the HTML link. ActionView::Helpers::UrlHelper.link_to calls url_for , but it calls the version of the AV module instead of the controller. I managed to get this to do what I intended by setting

  #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 this works. Can someone explain the explanation of the method and the hiding of what is happening here? What is the best way to mix in link_to (or, generally speaking, include only some methods from the module), so I can call it in the controller (generating a flash string with a link is a precedent.)

Please, no lectures on MVC - if anything, link_to should be in the module separately from url_for . Judging by the amount of noise on this, many people are confronted with this seemingly trivial grip and end up wasting an hour doing it in the “Rails way” when what is really needed is a minute hack to get my application working now. Is there a way to "Rails" do this with helpers? Or the best ruby ​​way?

+49
ruby-on-rails mixins helpers actionview link-to
01 Oct 2018-10-01T00:
source share
5 answers

This does not actually answer your question, but there is an easier way

For Rails 5 use proxy helpers

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

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

 # in the controller code view_context.link_to '...', '...' # instead of using your mixin code link_to '...', '...' 

For Rails 2 , since you use the helper from the controller, you can access the member variable @template of the controller, @template is a view, and UrlHelper is already mixed in it.

 # in the controller code @template.link_to '...', '...' # instead of using your mixin code link_to '...', '...' 

if you need to use urlhelper from code other than the controller, your solution will probably help you

+57
01 Oct '10 at 23:27
source share

Compatible with rails 3,4 and 5:

 view_context.link_to 
+113
Jun 19 '12 at 12:45
source share

You can do it as follows:

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

But in fact, the best place to create this link may be in the helper module, where UrlHelper utilities and other viewing-related helpers are already included.

[update]

This approach is deprecated and no longer works.

+1
May 18 '11 at 13:33
source share

I think the cleanest solution is to put the text in the view and use "render_to_string" from the controller.

0
Jul 06 2018-11-11T00:
source share

I still had problems using my own helper methods, which use the built-in helper methods in the controller with ActionController.helper.my_method.

Obviously using render_to_string for each flash will work, but I don't want to create so many small scores for each flash.

My solution was to create a small helper for controllers to execute code in partial.

Helper method in the controller:

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

Partially HAML (helper.html.haml):

 = instance_eval &block 

The same will work in ERB (helper.html.erb):

 <%= instance_eval &block %> 

Use this as in the controller to call the my_custom_helper_function function defined in the helper:

 redirect_to url, :notice => h{my_custom_helper_function} 
0
Jan 13 '12 at 11:32
source share



All Articles