首页 > 解决方案 > 获取“未定义的局部变量或方法错误”以仅在 Heroku 服务器上而非本地生成虾 pdf

问题描述

每当我在数据库中输入一条新记录时,虾都不会识别它,直到我重新启动我的 Heroku 服务器。我在本地(使用 Cloud9)没有问题,但是在使用 heroku 时出现如下错误:

NameError(未定义的局部变量或方法 `method_199'"

wheremethod_199没有找到,因为我刚刚将它添加到数据库中。如果我将我的代码重新部署到 heroku,那么它将识别method_199.

这似乎只是我的虾 pdf 的问题。在 heroku 网站上,数据库的其余部分似乎更新并响应得很好(这意味着我可以进行更新并且它显示而无需重新部署应用程序)

.rb

class ReportTwoPdf < Prawn::Document
 def initialize(property, current_user, start_date, end_date)
 super(top_margin: 50)

@property = property
@current_user = current_user
@start_date = start_date
@end_date = end_date

# @activity_types.each do |type|
# eval("test_#{type.id}")
# end


current_user.activity_types.each do |type|
  if @property.activities.where(activity_type_id: type.id).where(:date => 
  (@start_date..@end_date)).count > 0
    eval("method_#{type.id}")
  else
  end
end

end

ActivityType.all.each do |type|
define_method("method_#{type.id}") do
  move_down 20
  a = [1,
       type.subject_toggle == "Show" ? 1 : nil,
       type.contact_toggle == "Show" ? 1 : nil,
       type.agent_toggle == "Show" ? 1 : nil,
       type.customer_toggle == "Show" ? 1 : nil,
       type.detail_toggle == "Show" ? 1 : nil,
       type.outcome_toggle == "Show" ? 1 : nil,
       type.cost_toggle == "Show" ? 1 : nil,
       type.duration_toggle == "Show" ? 1 : nil].compact.length - 1

  font "Nunito"
  text type.title, :align => :left, size: 12, style: :bold
  move_down 5
  table eval("row_#{type.id}"), :position => :center, :width => 540, 
:column_widths => {0 => 50,1 => @min_width, a => 60},
                        :cell_style => {:font => "Nunito", :size => 9} do
    row(0).font_style = :bold
    columns(0..8).align = :center
    self.row_colors = ["F0F0F0", "FFFFFF"]
    self.header = true
  end
end
end

控制器:

 def create_pdf
@property = Property.find(params.fetch("id_to_display"))
@current_user = current_user
@start_date = Date.strptime(params.fetch("start_date"), "%Y-%m-%d")
@end_date = Date.strptime(params.fetch("end_date"), "%Y-%m-%d")
@user = current_user

respond_to do |format|
  format.html
  format.pdf do
    pdf = ReportTwoPdf.new(@property, @current_user, @start_date, @end_date)
    send_data pdf.render, :filename => "Report: #{@property.address}.pdf", :type => "application/pdf", disposition: "inline"
  end
end
end

宝石文件

gem "prawn"
gem 'prawn-table'

标签: ruby-on-railsprawnheroku-postgres

解决方案


这种行为是意料之中的,因为在生产中,类仅在服务器启动时评估一次。反过来,这通常仅在部署或扩展时发生。你的数据ActivityType显然比你部署的更频繁地改变

我不明白为什么需要定义这些动态方法,您可以像处理ActivityType任何其他数据一样处理 s:

class ReportTwoPdf < Prawn::Document
  def initialize(property, current_user, start_date, end_date)
    super(top_margin: 50)

    @property = property
    @current_user = current_user
    @start_date = start_date
    @end_date = end_date


    current_user.activity_types.each do |type|
      if @property.activities.where(activity_type_id: type.id).where(date: (@start_date..@end_date)).count > 0
        handle_activity_type(type)
      else
      end
    end

  end

  def some_row_similar(type)
    # here goes the code
  end

  def handle_activity_type(type)
    move_down 20
    a = [
      type.subject_toggle,
      type.contact_toggle,
      type.agent_toggle,
      type.customer_toggle,
      type.detail_toggle,
      type.outcome_toggle,
      type.cost_toggle,
      type.duration_toggle
    ].count{|toggle| toggle == "Show" }

    font "Nunito"
    text type.title, align: :left, size: 12, style: :bold
    move_down 5

    table some_row_similar(type), position: :center, width: 540, 
        column_widths: {0 => 50, 1 => @min_width, a => 60},
        cell_style: {font: "Nunito", size: 9} do
      row(0).font_style = :bold
      columns(0..8).align = :center
      self.row_colors = ["F0F0F0", "FFFFFF"]
      self.header = true
    end
  end

end

推荐阅读