In /app/models/review.rb
, I have the a method that's designed to create a PDF from the output of the review#print
action. Also designed to capture my entire print stylesheet and inline it in the header when I'm in production mode:
def create_pdf
snip # code that sets @competitors, @elements & @questions
css = Rails.application.assets.find_asset('print').to_s if Rails.env.production?
html = ActionController::Base.new.render_to_string "reviews/print", :locals => {:@review => self, :@competitors => @competitors, :@elements => @elements, :@questions => @questions, :@css => css}
pdf = HyPDF.new(html, :test => true)
upload = pdf.upload_to_s3("Testivate", [self.id, "_", Time.now.full_time, ".pdf"].join.downcase.gsub(" ", "_"), true)
self.update_attribute :latest_url, upload
NotificationMailer.pdf_creation(self).deliver
return html.to_s.truncate(300) # for debugging -- so I can see <head>
end
I have tried many variants of this code, including passing :layouts => false
and more to render
.
My reviews_controller.rb
contains:
class ReviewsController < ApplicationController
respond_to :html
filter_access_to :all
layout "print", :only => [:print, :pdf]
def print
snip # code that sets @review, @competitors, @elements @questions
respond_with(@review)
end
end
My app/views/reviews/print.html.haml
contains:
!!!
%html
%head
%title Testivate
- if @css.present?
= content_tag(:style, @css.html_safe, :type => "text/css")
- else
= stylesheet_link_tag "print", :media => "print, screen, projection"
Why, then, do I get the following error when I call @review.create_pdf
?
Rendered reviews/print.html.haml (366.3ms)
ActionView::Template::Error: ActionController::Metal#session delegated to @_request.session, but @_request is nil:
#<ActionController::Base:0x00000002bffd40
@_routes=nil,
@_action_has_layout=true,
@_headers={"Content-Type"=>"text/html"},
@_status=200,
@_request=nil,
@_response=nil,
@_prefixes=["action_controller/base"],
@_lookup_context=#<ActionView::LookupContext:0x00000002bec560 @details_key=#<ActionView::LookupContext::DetailsKey:0x000000096296a8 @hash=-2527668597365468702>,
@details={:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :haml]}, @skip_default_locale=false, @cache=true, @prefixes=["action_controller/base"],
@rendered_format=:html,
@view_paths=#<ActionView::PathSet:0x00000002bec4c0 @paths=[/app/app/views, /app/vendor/bundle/ruby/1.9.1/gems/declarative_authorization-0.5.7/app/views]>>,
@_view_renderer=#<ActionView::Renderer:0x00000002be4a18
@lookup_context=#<ActionView::LookupContext:0x00000002bec560 @details_key=#<ActionView::LookupContext::DetailsKey:0x000000096296a8 @hash=-2527668597365468702>,
@details={:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :haml]}, @skip_default_locale=false,
@cache=true,
@prefixes=["action_controller/base"],
@rendered_format=:html,
@view_paths=#<ActionView::PathSet:0x00000002bec4c0
@paths=[/app/app/views, /app/vendor/bundle/ruby/1.9.1/gems/declarative_authorization-0.5.7/app/views]>>, @_template_renderer=#<ActionView::TemplateRenderer:0x00000009637870 @lookup_context=#<ActionView::LookupContext:0x00000002bec560
@details_key=#<ActionView::LookupContext::DetailsKey:0x000000096296a8 @hash=-2527668597365468702>,
@details={:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :haml]}, @skip_default_locale=false,
@cache=true,
@prefixes=["action_controller/base"],
@rendered_format=:html,
@view_paths=#<ActionView::PathSet:0x00000002bec4c0
@paths=[/app/app/views, /app/vendor/bundle/ruby/1.9.1/gems/declarative_authorization-0.5.7/app/views]>>, @view=#<#<Class:0x00000002be4950>:0x000000096da390
@_config={},
@view_renderer=#<ActionView::Renderer:0x00000002be4a18 ...>,
@_routes=nil, @_assigns={"_routes"=>nil},
@_controller=#<ActionController::Base:0x00000002bffd40 ...>,
@_request=nil,
@view_flow=#<ActionView::OutputFlow:0x000000096da200 @content={}>, @output_buffer="",
@virtual_path="reviews/print",
@competitors=[#<Competitor id: 56, etc etc...>, @css="html,body,div,span,applet,object,iframe,etc ... (which is the css reset at the start of my stylseheet) ...on:none}\n",
etc
Update:
I tried creating the method from a Controller action on Review, rather than a class method, but I got the same ActionView::Template::Error:
error.
Update 2:
The view code as requested to check it's not trying to access session:
(I don't think it is.)
!!!
%html
%head
%title Testivate
- if @css.present?
= content_tag(:style, @css.html_safe, :type => "text/css")
- else
= stylesheet_link_tag "print", :media => "print, screen, projection"
= csrf_meta_tag
= analytics_init if Rails.env.production?
%body
.header
.imagebox
=image_tag "Testivate-logo.svg", :width => "300"
%h1#first Confidential Competitive Website Review & Benchmark
.imagebox
=image_tag @review.owner_logo.url
.body
%p Review conducted for #{@review.client_name}.
.header
%h1 Research overview
.body
%p The methodology that drives this research is #{@review.product.full_name}.
= raw @review.product.description
%p Between #{@review.created_at.full_date} and #{@review.updated_at.full_date}, Testivate tested and compared #{pluralize @review.competitors.count, "website"}:
- @competitors.sort{|c| c.name <=> c.name}.each do |competitor|
.imagebox
= image_tag competitor.logo.url(:thumb)
%h4 #{competitor.name}
%h4 #{competitor.url}
.header
%h1 Results summary
.body
%p #{pluralize @review.winners.count, "entrant"} led the review group with a score of #{@review.top_score}: #{@review.winners.map{|w| w.name}.join("; ")}.
%table
%thead
%tr
%th.not_controls Website
%th Score
%th.not_controls Website
%th Score
%tbody
- @competitors.each_slice(2).each do |competitor_pair|
%tr
%td.not_controls= competitor_pair.first.name
%td.not_controls= competitor_pair.first.current_score
%td.not_controls
- unless competitor_pair.count == 1
= competitor_pair.last.name
%td.not_controls
- unless competitor_pair.count == 1
= competitor_pair.last.current_score
.header
%h1 Tested website elements
.body
%p The research involve testing #{pluralize @elements.count, "element"}:
%table
%thead
%tr
%th Element:
- @elements.each do |element|
%th= element.name
%tbody
- @competitors.each do |competitor|
%tr
%td.first= competitor.name
- @elements.each do |element|
%td.not_controls= competitor.element_score(element)
.header
%h1 Website testing standards
.body
%p The research involve testing #{pluralize Standard.all.count, "standard"}:
%table
%thead
%tr
%th Standard:
- Standard.all.each do |standard|
%th= standard.name
%tbody
- @competitors.each do |competitor|
%tr
%td.first= competitor.name
- Standard.all.each do |standard|
%td.not_controls= competitor.standard_score(standard)
- @questions.each do |question|
.header
%h1 Question details
.body
%h3= question.name
%p Standard: #{question.standard.try(:name)}
%p Element: #{question.element.try(:name)}
%table
%thead
%tr
%th Competitor
%th Score
%th Explanation
%tbody
- @competitors.each do |competitor|
%tr
%td.first= competitor.name
%td.not_controls= competitor.current_score_for_question(question)
%td.not_controls= competitor.explanation_for_score_for_question(question)
- unless question.endmatter.blank?
%h3 References
%p To understand more about how this question was scored or how to fix any problems that were uncovered, consult the following references:
= raw question.endmatter
As Feckmore said, the problem usually comes from csrf_meta_tag inclusion at Layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With