重定向时 Datamapper 对象为零,刷新始终加载正常
我正在开发一个 Rails 应用程序,它在幕后进行一些绘图以生成各种数据集之间的比较。我看到的是在 DataMapper 数据库中生成 Comparison 对象时出现零星的 500 错误 (ActionView::Template::Error
)。它有时只发生在一台机器上,每次发生在另一台机器上,而从来没有发生在一台机器上。这些错误与计算机的性能相关,使我相信 DataMapper 在幕后做了一些奇怪的事情。
我已经放弃追寻“原因”,现在只是试图捕获 500 错误并强制刷新以防止审阅者看到问题。然而,我所尝试的一切都没有奏效。这是我的设置:
Comparisons_controller.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
这个错误已经困扰我好几个星期了,但并没有让我屈服。关于为什么或如何捕获错误并强制刷新有什么想法吗?
谢谢。
I'm developing a Rails app that does some plotting behind the scenes to generate a Comparison between various datasets. What I'm seeing is a sporadic 500 error (ActionView::Template::Error
) on generation of the Comparison object in my DataMapper database. It only happens sometimes on one machine, every time on another machine, and never on one. These errors correlate with the performance of the computer, leading me to believe that DataMapper is doing something weird behind the scenes.
I've given up on chasing down the 'why' and am now just trying to catch the 500 error and force a refresh to prevent the reviewers from seeing the problem. However, everything I've tried hasn't worked. Here's my setup:
comparisons_controller.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
This shows up in my production.log file:
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
This error has been bothering me for weeks, but has not yielded to me. Any ideas on the why or the how to catch the error and force a refresh?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在视图中加载
@comparison
,这是控制器的责任。您还注释掉了实际分配@comparison
的行,因此它的计算结果为nil
也就不足为奇了。请记住,在
create
上根本没有:id
参数。这可能解释了为什么它只在您最终获得可用信息时才适用于重定向。您的意思可能是这样的:
这将定义在视图中使用的变量,无论参数如何。
You shouldn't be loading
@comparison
inside your view, that's the responsibility of the controller. You've also commented out the line that actually assigns@comparison
so it's hardly surprising it's evaluating asnil
.Remember that on
create
there is no:id
parameter at all. This might explain why it only works on a redirect when finally you have that information available.What you probably meant was this:
This will define the variable for use inside your views regardless of the parameters.