我可能忽略了一些简单的事情,但我仍然无法找到问题。
控制器: (第一行是一个很长的思考斯芬克斯查询)
@artwork_q = Artwork.search params[:q], :conditions => {:subject => params[:artwork][:subject_ids], :location => params[:artwork][:location_ids], :artist_last => params[:artwork][:artist_id], :artist_first => params[:artwork][:artist_id]}, :order => params[:sort][:title]
@last_artwork = Artwork.new
@last_artwork = @artwork_q.last
render 'index2'
查看:
<% @artwork_q.each.with_index do |art, index| %>
<div class="a-result<%= " last-one" if art.id == @last_artwork.id %>" id="<%= index %>">
<% end %>
我测试了art.id
,并且工作正常。 @last_artwork.id
引发错误的是NoMethodError in Artworks#index2
Showing /Users/<user>/Developer/<project>/<rails_root>/app/views/artworks/index2.html.erb where line #49 raised:
undefined method `id' for nil:NilClass
:
PERSON
感谢您的帮助!
答案 0 :(得分:1)
[].last => nil
即您的查询未返回任何结果。在循环浏览之前,只需检查@artwork_q
是否有一些结果。
<% unless @artwork_q.blank? %>
<% @artwork_q.each.with_index do |art, index| %>
<div class="a-result<%= " last-one" if art.id == @last_artwork.id %>" id="<%= index %>">
<% end %>
<% end %>
答案 1 :(得分:0)
试试这个:
{{ entity.name }}
{{ entity.description }}