I have some complicated PDF generation logic that requires rendering a view outside of a controller and then passing the HTML into WickedPDF
:
ActionView::Base.send(:define_method, :protect_against_forgery?) { false }
av = ActionView::Base.new
av.view_paths = ActionController::Base.view_paths
income_statement_html = av.render :template => "reports/income_statement.pdf.erb", :layout => 'layouts/report.html.erb',
locals: {:@revenue_accounts => revenue_accounts,
:@expense_accounts => expense_accounts,
:@start_date => start_date,
:@end_date => end_date,
:@business => business}
This all works fine on Rails 4 but has stopped working when we upgraded to Rails 5.
All the instance variables we are setting here end up as nil
inside the view. Is there still a way to set instance variables from the render call like this?
:@revenue_accounts
seems pretty weird... try just::revenue_accounts
(and in your template, refer to them as local variables egrevenue_accounts
instead of the more global@revenue_accounts
) – Jerrybuilt