Using link_to in a class in a Rails helper

Mike Sutton picture Mike Sutton · Jan 7, 2010 · Viewed 7.4k times · Source

I have a rails helper using the structore below, but when I use it I get the message

undefined method 'link_to'

The helper is arranged as:

module MyHelper

  class Facet

    def render_for_search
      link_to("Value", params)
    end
  end

  class FacetList
    attr_accessor :facets

    def initialize
      #Create facets
    end

    def render_for_search
      result = ""
      facets.each do |facet|
        result << facet.render_for_search
      end
      result
    end
  end
end

Answer

mopoke picture mopoke · Jan 7, 2010

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.