user7806106
user7806106

Reputation:

Object must be a Date, DateTime or Time object. nil given

Object must be a Date, DateTime or Time object. nil given.
Extracted source (around line #23):                  

        <td><%= localize(movie.release_date, format: :long) %></td>

when I try to save with the blank date this error occurs, is there a strong textsolution where I can save without needing to fill it?

EDIT:

my browser

==========================================================================

my terminal

Upvotes: 2

Views: 2846

Answers (3)

Stefan
Stefan

Reputation: 114188

You can pass a default value to localize:

<td><%= localize(movie.release_date, format: :long, default: '-') %></td>

The default value is returned whenever the first argument is nil.

Upvotes: 3

Amadan
Amadan

Reputation: 198378

Your localize hates nil values, so don't give it one.

Hacky but short:

<td><%= movie.release_date && localize(movie.release_date, format: :long) %></td>

Nice and long:

<% unless movie.release_date.nil? %>
  <td><%= localize(movie.release_date, format: :long) %></td>
<% end %>

Upvotes: 1

Mori
Mori

Reputation: 27789

After you try to save movie and it fails, look at the resulting error, and also display the errors on that object, e.g.

movie.errors.full_messages

That should give you more details about why it failed.

Upvotes: 1

Related Questions