1

我正在开发一个 Rails 应用程序,它在幕后进行一些绘图以生成各种数据集之间的比较。我看到的是ActionView::Template::Error在我的 DataMapper 数据库中生成比较对象时出现零星的 500 错误 ()。它有时只会在一台机器上发生,每次都在另一台机器上发生,而永远不会在一台机器上发生。这些错误与计算机的性能相关,让我相信 DataMapper 在幕后做了一些奇怪的事情。

我已经放弃追查“为什么”,现在只是试图捕捉 500 错误并强制刷新以防止审阅者看到问题。但是,我尝试过的一切都没有奏效。这是我的设置:

比较控制器.rb

# This is the action which allows for generation of a new comparison.  It uses a fork to spawn a child process which generates the comparison. Notifications will appear when this fails or finishes.  
  def create
    first_set = get_msruns_from_array_of_ids(params[:comparison1].uniq)
    second_set = get_msruns_from_array_of_ids(params[:comparison2].uniq)

    comp = Comparison.new
    comp.msrun_firsts = first_set
    comp.msrun_seconds = second_set
    comp.first_description = params[:first_description]
    comp.second_description = params[:second_description]
    comp.save

# This should capture the windows fork and prevent it.
    if RbConfig::CONFIG['host_os'] === 'mingw32'
      redirect_to :action => "show", :id => comp.id
      result = comp.graph
      a = Alert.create({ :email => false, :show => true, :description => "DONE WITH COMPARISON #{comp.id}" })
    else
      fork do
        result = comp.graph
        a = Alert.create({ :email => false, :show => true, :description => "DONE WITH COMPARISON #{comp.id}" })
      end
      flash[:notice] = "Comparison started. You will be notified when it completes."
 # HERE I've attempted to capture problem 
      begin 
        render :action => "show"
      rescue 
        redirect_to :action => "show", :id => comp.id
      end 
    end

这显示在我的 production.log 文件中:

ActionView::Template::Error (undefined method `first_description' for nil:NilClass):  
    1: /- @comparison ||= Comparison.get(params[:id])
    2: /%h1= "Comparison ##{@comparison.id}"
    3: %p <strong>User Description: </strong>
    4: <p> Set#1: #{ @comparison.first_description }
    5: <p> Set#2: #{@comparison.second_description }
    6: <p> #{link_to "Edit", edit_comparison_path(@comparison)}
    7: %ul.categories
  app/views/comparisons/show.html.haml:4

这个错误已经困扰我好几个星期了,但并没有让我屈服。关于为什么或如何捕获错误并强制刷新的任何想法?

谢谢。

4

1 回答 1

1

您不应该@comparison在视图中加载,这是控制器的责任。您还注释掉了实际分配的行,@comparison因此它评估为 也就不足为奇了nil

请记住, oncreate根本没有:id参数。这可以解释为什么它只在您最终获得该信息时才适用于重定向。

你的意思可能是这样的:

@comparison = Comparison.new

无论参数如何,这将定义在视图中使用的变量。

于 2012-01-30T17:16:09.167 回答