Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 > Rendering views in rake task

I'm stuck with a rake task that need to prepare a newsletter for Mailchimp.

Using rails 2.x stuff googled I now have this code:

desc "Sends newsletter to Mailchimp list"
  task :send_newsletter => :environment do
    begin
      # get render helpers
      av = ActionView::Base.new(Rails::Application::Configuration.new(Rails.root).view_path)
      av.class_eval do
        include ApplicationHelper
      end

      things = Stuff.do.things

      h = Hominid::Base.new({:api_key => "xxx"})
      h.create_campaign(
        {
          :list_id => "xxx",
          :subject => "Hey...",
          :from_email => "xxx",
          :from_name => "xxx",
          :to_email => "",
          :auto_footer => true,
          :generate_text => true
        },
        {
          :html => av.render(:template => "stuff/newsletter", :locals => {:things => things}, :layout => false)
        },
        "regular")
    rescue Exception => e
      STDERR.puts ">>> #{e.to_yaml}"
    end

And I get this error message: "undefined method `virtual_path' for false:FalseClass"

My first try was with render_to_string but I just can't access as it is in the controller not the view.

Any help would be greatly appreciated :)

like image 638
jbfeldis Avatar asked Sep 20 '10 14:09

jbfeldis


1 Answers

:layout => nil ?

like image 121
gertas Avatar answered Oct 18 '22 01:10

gertas