ruby-on-railslink-tourl-helper

undefined method 'link_to'


I'm writing a ruby-on-rails library module:

module Facets

  class Facet
    attr_accessor :name, :display_name, :category, :group, :special

    ...

    URI = {:controller => 'wiki', :action => 'plants'}
    SEARCH = {:status => WikiLink::CURRENT}

    #Parameters is an hash of {:field => "1"} values
    def render_for_search(parameters)
    result = link_to(display_name, URI.merge(parameters).merge({name => "1"}))
    count = WikiPlant.count(:conditions => (SEARCH.merge(parameters.merge({name => "1"}))))
    result << "(#{count})"
    end
  end

  ...

end

when I call render_for_search I get the error

undefined method 'link_to'

I've tried requiring url_helper directly but can't figure out what's going wrong.


Solution

  • This is because, ActionView urlhelpers are only available to the Views, not in your lib directory.

    the link_to method is found in the ActionView::Helpers::UrlHelper module, plus you wou

    so try this.

     class Facet
       include ActionView::Helpers::UrlHelper
    ...
    end