繁体   English   中英

ActionView :: Template :: Error(对于nil:NilClass,未定义的方法“ to_formatted_s”):在Heroku上,但不是Localhost

[英]ActionView::Template::Error (undefined method `to_formatted_s' for nil:NilClass): on Heroku but Not Localhost

我让此页面在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帖子,但是它们都没有解决问题。 有人可以针对这种情况提供任何指导吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM