首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Datamapper对象在重定向时为空,刷新始终正常加载

Datamapper对象在重定向时为空,刷新始终正常加载
EN

Stack Overflow用户
提问于 2012-01-31 01:08:36
回答 1查看 136关注 0票数 1

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

我已经放弃了追查“为什么”,现在只是试图捕捉500错误,并强制刷新,以防止审查者看到问题。然而,我尝试的所有东西都不起作用。下面是我的设置:

comparisons_controller.rb

代码语言:javascript
运行
复制
# 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文件中:

代码语言:javascript
运行
复制
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

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

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-31 01:16:09

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

请记住,在create上根本没有:id参数。这可能解释了为什么只有当你最终拥有可用的信息时,它才在重定向上起作用。

你的意思可能是:

代码语言:javascript
运行
复制
@comparison = Comparison.new

这将定义在视图中使用的变量,而不管参数是什么。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9067759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档