Using link_to in a class in a Rails helper Using link_to in a class in a Rails helper ruby-on-rails ruby-on-rails

Using link_to in a class in a Rails helper


Try using this:

self.class.helpers.link_to

Because link_to is not defined in your current scope.

The above will work for a controller, but I'm guessing it will work inside another helper as well. If not then try:

include ActionView::Helpers::UrlHelper

At the top of your helper.


This is because within the Class Facet you don't have access to the template binding.In order to call the render_for_search method you probably do something like

<%= Facet.new.render_for_search %>

Just override your initialize method to take the current context as argument.The same applies to the params hash.

class Facet  def initialize(context)    @context = context  end  def render_for_search    @context.link_to("Value", @context.params)  endend

Then call

<%= Facet.new(self).render_for_search %>

Otherwise, define the render_for_search method directly within the MyHelper module and don't wrap it into a class.