0

在 localhost:3000 上加载没有问题,但是一旦部署到 Heroku,我就会收到以下错误。冉

heroku logs --tail

2013-08-29T04:38:23.403690+00:00 app[web.1]: Connecting to database specified by DATABASE_URL
2013-08-29T04:38:26.898295+00:00 app[web.1]: [2013-08-29 04:38:26] INFO  WEBrick 1.3.1
2013-08-29T04:38:26.898295+00:00 app[web.1]: [2013-08-29 04:38:26] INFO  ruby 2.0.0 (2013-06-27) [x86_64-linux]
2013-08-29T04:38:26.898634+00:00 app[web.1]: [2013-08-29 04:38:26] INFO  WEBrick::HTTPServer#start: pid=2 port=11226
2013-08-29T04:38:27.010520+00:00 heroku[web.1]: State changed from starting to up
2013-08-29T04:38:28.496863+00:00 app[web.1]: Started GET "/" for 50.148.15.124 at 2013-08-29 04:38:28 +0000
2013-08-29T04:38:28.642277+00:00 app[web.1]: Processing by EventsController#index as HTML
2013-08-29T04:38:29.529140+00:00 heroku[router]: at=info method=GET path=/ host=afternoon-gorge-1648.herokuapp.com fwd="50.148.15.124" dyno=web.1 connect=5ms service=1081ms status=500 bytes=643
2013-08-29T04:38:29.541662+00:00 app[web.1]:   Rendered events/_event.html.erb (320.3ms)
2013-08-29T04:38:29.541795+00:00 app[web.1]:   Rendered events/index.html.erb within layouts/application (743.9ms)
2013-08-29T04:38:29.542093+00:00 app[web.1]: Completed 500 Internal Server Error in 900ms
2013-08-29T04:38:29.544607+00:00 app[web.1]: 
2013-08-29T04:38:29.544607+00:00 app[web.1]: ActionView::Template::Error (undefined method `name' for nil:NilClass):
2013-08-29T04:38:29.544607+00:00 app[web.1]:     3:   <td><%= event.title %></td>
2013-08-29T04:38:29.544607+00:00 app[web.1]:     4:   <td><%= event.location %></td>
2013-08-29T04:38:29.544607+00:00 app[web.1]:     5:   <td><%= event.description %></td>
2013-08-29T04:38:29.544607+00:00 app[web.1]:     6:   <td><strong><%= link_to event.user.name, event.user %></strong></td>
2013-08-29T04:38:29.544607+00:00 app[web.1]:     7:   <td><%= link_to 'Show', event %></td>
2013-08-29T04:38:29.544607+00:00 app[web.1]:     8:   <% if current_user == event.user %>
2013-08-29T04:38:29.544607+00:00 app[web.1]:     9:     <td><%= link_to 'Edit', edit_event_path(event) %></td>
2013-08-29T04:38:29.544607+00:00 app[web.1]:   app/views/events/_event.html.erb:6:in `_app_views_events__event_html_erb___4373324357052961172_69914882818260'
2013-08-29T04:38:29.544790+00:00 app[web.1]:   app/views/events/index.html.erb:20:in `_app_views_events_index_html_erb___3028579038251132182_69914889655380'
2013-08-29T04:38:29.544790+00:00 app[web.1]:   app/controllers/events_controller.rb:9:in `index'

我已经确认我的数据库在本地和 Heroku 上匹配。该应用程序仅在登录时失败。登录页面在注销时加载正常。当我在我的数据库中添加新的 7 个新用户字段时发生这种情况,所有字符串。冉

rake db:migrate 

本地。

heroku run rake db:migrate.

这是 Heroku 链接: http ://afternoon-gorge-1648.herokuapp.com/

非常困惑问题出在哪里。

更新:这是事件索引页面:

<% if user_signed_in? %>
  <div class="hero-unit">
    <h1>Sign up for any of the following events!</h1>
  </div>
  <h1>Listing events</h1>
  <table class="table table-striped">
    <thead>
      <tr>
        <th>Image</th>
        <th>Title</th>
        <th>Location</th>
        <th>Description</th>
        <th>Posted by</th>
        <th></th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody>
     <%= render @events %>
    </tbody>
  </table>
  <br/>
  <%= link_to 'Add New Event', new_event_path, class: "btn btn-primary btn-large" %>
<% else %>
  <%= render 'pages/home' %>
<% end %> 

调用 _event.html.erb 部分:

<tr>
  <td><%= image_tag event.image(:medium) %></td>
  <td><%= event.title %></td>
  <td><%= event.location %></td>
  <td><%= event.description %></td>
  <td><strong><%= link_to event.user.name, event.user %></strong></td>
  <td><small><%= link_to 'Show', event %></small></td>
  <% if current_user == event.user %>
    <td><small><%= link_to 'Edit', edit_event_path(event) %></small></td>
    <td><small><%= link_to 'Delete', event, method: :delete, data: { confirm: 'Are you sure?' } %></small></td>
  <% end %>
</tr>
4

2 回答 2

0

I assume event is coming through as one object of many from a block (something like @events.each do |event|...). Do you have bad seed data where it's producing an event row with no data? You could run heroku run rails c and then events = Event.all and see if there is strange looking data (nulls) in there.

EDIT: It would also be helpful to see more of the error. Is name being called on an event object or something else?

于 2013-08-29T04:38:19.300 回答
0

您正在尝试调用渲染部分速记。但是,速记只能用于单个对象,但您正在尝试传递 _a 对象的集合。相反,您需要循环@events并将每个结果event作为局部结果传递给您的局部:

# app/views/events/index.html.erb
<% @events.each do |event| %>    
    <%= render :partial => 'events', :locals => {:event => event} %>
<% end %>

要了解有关您调用的语法为何不起作用的更多信息,请参阅Rails 指南中的第3.4.4 节传递局部变量。基本上,partial 期望一个单一的对象(它将通过局部变量访问),但是相反,您将其传递给多个对象的集合。因此,虽然局部变量可用于局部变量,但不是。_event.html.erbeventEventeventsevent

由于您仍在尝试访问event变量(不存在)的属性/关系,因此您会收到NilClass错误消息。因此,遍历每个事件并将该事件显式传递给部分应该可以解决您的错误。

于 2013-08-29T05:45:57.060 回答