Render a view from a rake task
Asked Answered
E

0

1

I'm trying to send mail from a rake task. I've already manage to send email but the problem is the file I send. I want to send a view which will be modify each time that the mail will be send.

I've install the gem render_anywhere to render a view from my rake task but I keep having this error :

NoMethodError: undefined method `formats' for String:0x007ff1a1068b88>

here is the part of my rake task that should do the job :

require 'render_anywhere' include RenderAnywhere require Rails.root.join('app', 'helpers', 'reports_helper')

namespace :reports do desc 'Example of writing a view to a file' task :example => :environment do

class RakeActionView < ActionView::Base
  include Rails.application.routes.url_helpers
  include ::ApplicationHelper

  def default_url_options
    {host: 'localhost:3000'}
  end

  def protect_against_forgery?
    false
  end
end

controller = ActionController::Base.new
controller.class_eval do
  include Rails.application.routes.url_helpers
  include ::ApplicationHelper
end


controller.request = ActionDispatch::TestRequest.new

view = RakeActionView.new(Rails.root.join('app', 'views'), {}, controller)

view.assign( :user => Report.first )


html = view.render(:template =>"#{Rails.root}/app/views/reports/organize.html.erb", layout: false)



pdf = PDFKit.new(html)
pdf.stylesheets << "#{Rails.root}/app/assets/stylesheets/print/reports-print.css.scss"
Ewing answered 9/6, 2015 at 7:5 Comment(1)
This solution except pdf part works for me in rails 4.2 against among othersArgus

© 2022 - 2024 — McMap. All rights reserved.