Trouble rendering a view inside a generic class

Posted by Horace Loeb on Stack Overflow See other posts from Stack Overflow or by Horace Loeb
Published on 2010-06-18T00:52:44Z Indexed on 2010/06/18 4:13 UTC
Read the original article Hit count: 268

Filed under:
|

I'm trying to encapsulate the logic for generating my sitemap in a separate class so I can use Delayed::Job to generate it out of band:

class ViewCacher
  include ActionController::UrlWriter

  def initialize
    @av = ActionView::Base.new(Rails::Configuration.new.view_path)
    @av.class_eval do
      include ApplicationHelper      
    end
  end

  def cache_sitemap
    songs = Song.all

    sitemap = @av.render 'sitemap/sitemap', :songs => songs
    Rails.cache.write('sitemap', sitemap)
  end
end

But whenever I try ViewCacher.new.cache_sitemap I get this error:

ActionView::TemplateError: 
ActionView::TemplateError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.url_for) on line #5 of app/views/sitemap/_sitemap.builder:

I assume this means that ActionController::UrlWriter is not included in the right place, but I really don't know

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rendering