Rails 3>在rake任务中渲染视图

时间:2010-09-20 14:34:20

标签: ruby-on-rails ruby-on-rails-3 rendering rake render

我遇到了需要为Mailchimp准备时事通讯的rake任务。

使用rails 2.x stuff googled我现在有了这段代码:

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

我收到此错误消息:“undefined method`virtual_path'for false:FalseClass”

我的第一次尝试是使用render_to_string,但我无法访问,因为它在控制器中而不是视图。

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:12)

:layout => nil