繁体   English   中英

Heroku 问题,ActionView::Template::Error(nil:NilClass 的未定义方法“名称”):

[英]Heroku issue, ActionView::Template::Error (undefined method `name' for nil:NilClass):

在 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>

我假设event是作为一个块中的一个对象出现的(类似于@events.each do |event|... )。 您是否有错误的种子数据,它产生了一个没有数据的事件行? 你可以运行heroku run rails c然后events = Event.all看看那里是否有奇怪的数据(空值)。

编辑:查看更多错误也会有所帮助。 是在event对象上调用name还是其他什么?

您正在尝试调用渲染部分简写。 但是,简写只能用于单个对象,但您正在尝试传递 _a collectionio_n 对象。 相反,您需要遍历@events并将每个结果event作为局部event传递给您的局部event

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

要了解有关您调用的语法为何不起作用的更多信息,请参阅Rails 指南中的第3.4.4传递局部变量 基本上, _event.html.erb部分期待一个奇异的对象(它通过访问event局部变量),而是要传递它的多个集合Event的对象。 因此,虽然局部变量events可用于部分,但event不是。

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

暂无
暂无

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

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