I'm probably overlooking something simple here but I still haven't been able to find the problem.
Controller: (First line is a long Thinking Sphinx query)
@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'
View:
<% @artwork_q.each.with_index do |art, index| %>
<div class="a-result<%= " last-one" if art.id == @last_artwork.id %>" id="<%= index %>">
<% end %>
I tested art.id
and that's working fine. It's the @last_artwork.id
that's raising the error which is this:
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
Thank you for any help!
[].last => nil
i.e. your query returns no results. Simply check @artwork_q
has some results in before looping through them.
<% 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 %>