Rails 3 > Rendering views in rake task

蹲街弑〆低调 提交于 2019-12-06 19:54:47

问题


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 ?



来源:https://stackoverflow.com/questions/3752454/rails-3-rendering-views-in-rake-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!