4

/app/models/review.rb中,我有一种方法,旨在从review#print操作的输出创建 PDF。还旨在捕获我的整个打印样式表,并在我处于生产模式时将其内联到标题中:

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  

我已经尝试过此代码的许多变体,包括传递:layouts => falserender.

我的reviews_controller.rb包含:

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

我的app/views/reviews/print.html.haml包含:

!!!
%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"

那么,为什么我在调用时会收到以下错误@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

更新:

我尝试从 Review 上的 Controller 操作而不是类方法创建方法,但我得到了同样的ActionView::Template::Error:错误。

更新 2:

请求查看代码以检查它是否未尝试访问会话:

(我认为不是。)

!!!
%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
4

1 回答 1

7

正如 Feckmore 所说,问题通常来自布局中的 csrf_meta_tag 包含。

于 2015-06-25T08:26:32.327 回答