ActionView :: Template :: Error(未定义的方法`to_formatted_s'为nil:NilClass):在Heroku上但不是Localhost

时间:2016-09-04 04:42:52

标签: ruby-on-rails heroku

我在localhost上正常使用此页面,但当我推送到Heroku时,我收到以下错误:

2016-09-04T04:35:37.745015+00:00 app[web.1]: Completed 500 Internal Server Error in 8ms (ActiveRecord: 2.6ms)
2016-09-04T04:35:37.745843+00:00 app[web.1]: 
2016-09-04T04:35:37.745844+00:00 app[web.1]: ActionView::Template::Error (undefined method `to_formatted_s' for nil:NilClass):
2016-09-04T04:35:37.745845+00:00 app[web.1]:     35:       <% @occasions.each do |o| %>
2016-09-04T04:35:37.745846+00:00 app[web.1]:     36:         <tr>
2016-09-04T04:35:37.745847+00:00 app[web.1]:     37:           <td>
2016-09-04T04:35:37.745848+00:00 app[web.1]:     38:             <%= o.date.to_formatted_s(:long) %>
2016-09-04T04:35:37.745848+00:00 app[web.1]:     39:           </td>
2016-09-04T04:35:37.745849+00:00 app[web.1]:     40:           <td>
2016-09-04T04:35:37.745850+00:00 app[web.1]:     41:             <%= link_to occasion_path(o), style: "color: black" do %>

它所在的erb页面上写着:

<%= image_tag 'memory_man.png', class: "slideRight", style: "width: 60%; right: 0; bottom: 0; position: absolute; visibility: hidden" %>

<div class="row" style="height: 125px"></div>

<% if flash[:notice] %>
  <div class="alert alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <%= flash[:notice] %>
  </div>
<% elsif flash.now[:alert] %>
  <div class="alert alert-danger">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <%= flash.now[:alert] %>
  </div>
<% elsif flash[:alert] %>
  <div class="alert alert-warning">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <%= flash[:alert] %>
  </div>
<% end %>

<div class="col-xs-12">
  <h1 class="text-center" style="margin-bottom: 15px">Your External Memory</h1>
  <% if @occasions.count == 0 %>
    <h4>If you don't trust us, we can't help you...</h4>
  <% else %>
    <table>
      <tr>
        <th>Date</th>
        <th>Thing to Remember</th>
        <th>Reapeats?</th>
        <th>Gift Needed?</th>
        <th>Email Reminders?</th>
      </tr>
      <% @occasions.each do |o| %>
        <tr>
          <td>
            <%= o.date.to_formatted_s(:long) %>
          </td>
          <td>
            <%= link_to occasion_path(o), style: "color: black" do %>
              <strong><%= o.name %></strong>
            <% end %>
            <%= link_to edit_occasion_path(o) do %>
              <span class="glyphicon glyphicon-edit" style="margin: 0" aria-hidden="true"></span>
            <% end %>
            <%= link_to occasion_path(o), method: :delete, data: { confirm: 'Are you sure?' } do %>
              <span class="glyphicon glyphicon-remove" style="margin: 0" aria-hidden="true"></span>
            <% end %>
          </td>
          <td>
            <%= o.repeating %>
          </td>
          <td>
            <% if o.gift %>
              Yup.
            <% else %>
              Nope!
            <% end %>
          </td>
          <td>
            <% if o.reminder %>
              Yes, fear not.
            <% else %>
              You're on your own.
            <% end %>
          </td>
        </tr>
      <% end %>
    </table>
  <% end %>
  <h4 class="text-center"><%= link_to "Add to My Internet-Braintrust", new_occasion_path, class: "hvr-grow btn btn-manly" %>
</div> <!-- container/columns -->

我查看了其他类似Heroku与localhost问题的SO帖子,但没有一个解决了这个问题。有人可以就这种情况提供任何指导吗?

1 个答案:

答案 0 :(得分:0)

看起来o.date为零,你应该找出哪个记录​​包含一个nil列日期并分配一个值。或者您可以更改代码:

o.date.try(:to_formatted_s, :long)