ruby-on-rails - 是什么导致了这个错误? ActionView::Template::Error: ActionController::Metal#session 委托(delegate)给@_request.session,但@_request 为零

标签 ruby-on-rails views rendering

/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 => false更多信息请访问 render .

我的 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:

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

(我认为不是。)
!!!
%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

最佳答案

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

关于ruby-on-rails - 是什么导致了这个错误? ActionView::Template::Error: ActionController::Metal#session 委托(delegate)给@_request.session,但@_request 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17827351/

相关文章:

image-processing - 反向(移除)抗混叠滤波器

java - 如何: JTable look and feel like Windows 7 Details View or Vuze table

ruby-on-rails - 运行 rspec 时如何让 Rails.logger 打印到控制台/stdout?

ruby-on-rails - 向设计模型添加新字段。 rails 5

ruby-on-rails - 渲染部分 Assets

mysql - 在 MySQL 中使用 View 的优势

sql-server - SQL Server 索引 View 与 Oracle 物化 View

python - 在 Django PermissionDenied 中发送自定义消息

ruby-on-rails - 使用 <% provide( :title, “Page Title” ) %> 的各种错误

c - OpenGL glReadPixels 到 FreeImage 位图