jlfenaux
jlfenaux

Reputation: 3291

Accessing Route helpers in rails 3.2

I'm migrating a rails 2.3 app to rails 3.2. I have an object used to manage the caching of a few stats I collect from different sources. As the collection process is quite long, It couldn't be done with the traditional fragment caching. I had to fill the cache asynchronously. the AsynchCache object, get data from Google Analytics and stores the content of a partial 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 for the display is as follows

When I want to display the content, I just have to use :

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

It works fine when I first load the page, or in irb, but if I try to reload the page or set in development.rb config.cache_classes = true, I get an 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 acccess to the routes.

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

Upvotes: 3

Views: 2068

Answers (1)

FrontierPsycho
FrontierPsycho

Reputation: 743

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

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

Upvotes: 9

Related Questions