Rails .each except if
I have a rails table called Movies. Movies are being collected and saved
from an API which means that some movies may have a release_date and some
may not.
All Movies are being displayed on the home page and they are sorted by
{|t| - t.release_date.strftime("%Y%m%d").to_i}
<% @movies.sort_by{|t| - t.release_date.strftime("%Y%m%d").to_i}.each do
|movie| %>
<% movie.title %>
<% movie.release_date.strftime("%Y") %>
<% end %>
So this code works fine but only as long as the returned movies have a
release date. If they don't have a release date assigned, it gives me the
following error.
ActionView::Template::Error (undefined method `strftime' for nil:NilClass):
But im only getting this error if the movie has no release_date. So how
can i add an exception to only display films WITH a release_date, where
using strftime would no longer be a problem.
I've tried
<% unless movie.release_date.blank? %>
<% @movies.sort_by{|t| - t.release_date.strftime("%Y%m%d").to_i}.each do
|movie| %>
<% @movie.title %>
<% @movie.release_date.strftime("%Y") %>
<% end %>
<% end %>
But that doesn't work as it gives an undefined method for 'movie'
No comments:
Post a Comment