0

遗留 Ruby on Rails 2.3 项目在这里。收到以下错误:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

提取的源代码(在 #122 行附近):

119: <%= link_to 'Printable', { :action => 'print', :id => @incident.id }, { :target => '_blank', :class => "button" } %>
120: <% if isviewable?(@incident) %>
121: 
122:   <%= link_to "Pictures (#{@incident.pictures.count})", incident_pictures_path(@incident), :class => "button" %>
123:   <%= link_to "Suspects (#{@incident.suspects.count})", incident_suspects_path(@incident), :class => "button" %>
124:   <%= link_to "Notes (#{@incident.notes.count})", incident_notes_path(@incident), :class => "button"%>
125:   <%= link_to "Cars (#{@incident.cars.count})", incident_cars_path(@incident), :class => "button" %>

有什么想法吗?谢谢。

编辑:删除第 122 行修复了异常,但如果该记录有,我可能需要显示一张图片,所以我不能只删除该功能。

编辑2:应用程序跟踪下的异常页面中也有这一行:

/usr/local/rvm/gems/ruby-1.8.7-p371/gems/paperclip-2.3.0/lib/paperclip.rb:303:in `validates_attachment_content_type'

我的回形针插件可能有错误吗?

4

2 回答 2

0
119: <%= link_to 'Printable', { :action => 'print', :id => @incident.id }, { :target => '_blank', :class => "button" } %>
    <% if @incident.present?%>       #### you need to check for null object. ######
120: <% if isviewable?(@incident) %>
121:
122:   <%= link_to "Pictures (#{@incident.pictures.count})", incident_pictures_path(@incident), :class => "button" %>
123:   <%= link_to "Suspects (#{@incident.suspects.count})", incident_suspects_path(@incident), :class => "button" %>
124:   <%= link_to "Notes (#{@incident.notes.count})", incident_notes_path(@incident), :class => "button"%>
125:   <%= link_to "Cars (#{@incident.cars.count})", incident_cars_path(@incident), :class => "button" %>

您需要检查空对象,然后希望它可以正常工作。

于 2013-05-14T14:58:25.827 回答
0

尝试在控制器中使用 .find_by_id 而不是 .find。然后,您可以在继续之前检查结果(@incident.nil?)。

编辑:

如何@incident.pictures?检查是否有?

于 2013-05-14T14:59:01.693 回答