Rails 3 > Rendering views in rake task

2020-08-17 18:22发布

问题:

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 :)

回答1:

:layout => nil ?