Undefined method 'url_for' while rendering a Rails 2.3.x template inside a ActiveRecord model

3k views Asked by At

I know that this not an "acceptable" practice but as a background process I am caching a rendered Rails partial into the database for faster output via JSONP. I have found many resources on the topic and successfully output a Rails 2.3.x partial within a ActiveRecord model, BUT if I use dynamic URLs within a partial Rails blows up.

I get a successful output (minus the dynamic URLs) with this...

def self.compile_html  

  viewer = Class.new(ApplicationController)
  path = ActionController::Base.view_paths rescue ActionController::Base.view_root

  Question.all.each do |q|
  q.html = ActionView::Base.new(path, {}, viewer).render(:partial => "questions/cached_output", :locals => { :question => q })  
  sleep 1 # hold for complete output and debugging only
  q.save( false ) # bypass validations
  end
  return "Caching complete"
end

I have tried including ActionController::UrlWriter Modules and ActionView::Helpers::UrlHelper with no luck. I cannot seems to find the instantiation/inclusion order to allow the partial to access ActionController::Base.new.url_for() to write the dynamic routes to the partial.

I did attempt a similar process as RoR: undefined method `url_for' for nil:NilClass without luck

I know that render_anywhere is a solution for Rails 3.x apps, but I am working in Rails 2.3.11

Thoughts?

1

There are 1 answers

2
Brett Bender On

I have an old app that I had to do something similar (but made use of polymorphic_path and polymorphic_url) in a model.

The following includes gave me access to url_for and polymorphic_path etc.

include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionController::PolymorphicRoutes
include ActionController::UrlWriter