Accessing route helpers in rails 3.2

I am porting a rails 2.3 application to rails 3.2. I have an object used to manage the caching of multiple statistics that I am collecting from various sources. Since the data collection process is quite long, this cannot be done with traditional fragment caching. I had to fill the cache asynchronously. an AsynchCache object, receive data from Google Analytics, and store the content of the partial content in the cache.

include Rails.application.routes.url_helpers

class AsynchCache 

  def self.get_popular_races_from_google_analytics(lang='en')
  # getting info form GA using Garb.
  end

def self.set_popular_races(language=nil)
   av = ActionView::Base.new(Rails.configuration.paths['app/views'])
    language_list = Global.instance.languages_UI.map{|a| a[0]}
    for lang in language.nil? ? language_list : [language]
      output = av.render(
          :partial => "home/popular_races",
          :locals => {:lang => lang}
      )
      Rails.cache.delete("popular_races_#{lang}")
      Rails.cache.write("popular_races_#{lang}",output) 
    end
  end

 def self.get_popular_races(lang='en')
    Rails.cache.read("popular_races_#{lang}")
  end
end

      

The partial I used to display is as follows

            
  •                                   
  • "> tmp_race.name -%>                             
  

When I want to display content, I just need to use:

<div id="popular_races"> <%# AsynchCache.get_popular_races(params[:locale])-%> </div>

      

It works fine on first page load or on irb, but if I try to reload the page or set in development.rb config.cache_classes = true I get the error:

stack level too deep
Rails.root: /Users/macbook/Sites/marathons

Application Trace | Framework Trace | Full Trace
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:70

      

If I remove, include Rails.application.routes.url_helpers, I don't get the error, but of course I don't have access to routes.

Is there a new way to get route methods from a model in rails 3.2?

+3


source to share


1 answer


I had the same problem as you. What I did, instead of including helpers, I used them like this every time:



Rails.application.routes.url_helpers.object_path(...)

+9


source







All Articles