Heroku ActionView :: Template :: Error(未定义的方法`name'代表nil:NilClass)

时间:2017-03-13 06:43:30

标签: ruby-on-rails ruby heroku

第一次在这里张贴。当我尝试通过heroku打开它时,我的网络应用程序出了问题。该应用程序由Heroku成功构建,但当我尝试打开应用程序时,我收到以下错误:

2017-03-13T06:15:35.778714+00:00 app[web.1]: I, [2017-03-13T06:15:35.778662 #4]  INFO -- : [773bef2b-dfb4-45a9-a15f-0de210ab6964] Completed 500 Internal Server Error in 232ms (ActiveRecord: 18.7ms)
2017-03-13T06:15:35.779870+00:00 app[web.1]: F, [2017-03-13T06:15:35.779817 #4] FATAL -- : [773bef2b-dfb4-45a9-a15f-0de210ab6964]   
2017-03-13T06:15:35.780386+00:00 app[web.1]: F, [2017-03-13T06:15:35.779874 #4] FATAL -- : [773bef2b-dfb4-45a9-a15f-0de210ab6964] ActionView::Template::Error (undefined method `name' for nil:NilClass):
2017-03-13T06:15:35.780553+00:00 app[web.1]: F, [2017-03-13T06:15:35.780501 #4] FATAL -- : [773bef2b-dfb4-45a9-a15f-0de210ab6964]      7:       <tr>    
2017-03-13T06:15:35.780554+00:00 app[web.1]: [773bef2b-dfb4-45a9-a15f-0de210ab6964]      8:             <% row_cities.each do |city| %>     
2017-03-13T06:15:35.780555+00:00 app[web.1]: [773bef2b-dfb4-45a9-a15f-0de210ab6964]      9:                 <td>
2017-03-13T06:15:35.780556+00:00 app[web.1]: [773bef2b-dfb4-45a9-a15f-0de210ab6964]     10:                     <h3><%= city.name if city.name %></h3>
2017-03-13T06:15:35.780557+00:00 app[web.1]: [773bef2b-dfb4-45a9-a15f-0de210ab6964]     11:                     <p><%= city.country %></p>
2017-03-13T06:15:35.780558+00:00 app[web.1]: [773bef2b-dfb4-45a9-a15f-0de210ab6964]     12:                     <%= link_to "See More", city_path(city) %>
2017-03-13T06:15:35.780559+00:00 app[web.1]: [773bef2b-dfb4-45a9-a15f-0de210ab6964]     13:                 </td>
2017-03-13T06:15:35.780602+00:00 app[web.1]: F, [2017-03-13T06:15:35.780550 #4] FATAL -- : [773bef2b-dfb4-45a9-a15f-0de210ab6964]   
2017-03-13T06:15:35.780655+00:00 app[web.1]: F, [2017-03-13T06:15:35.780610 #4] FATAL -- : [773bef2b-dfb4-45a9-a15f-0de210ab6964] app/views/home_page/home.html.erb:10:in `block (2 levels) in _app_views_home_page_home_html_erb__1188228438750051727_70102451451540'

以下是有问题的代码(views:home.html.erb):

<div class="container-fluid text-center">
    <table>
    <% @cities.in_groups_of(3) do |row_cities| %>
        <tr>    
            <% row_cities.each do |city| %>     
                <td>
                    <h3><%= city.name %></h3>
                    <p><%= city.country %></p>
                    <%= link_to "See More", city_path(city) %>
                </td>
            <% end %>
        </tr>
    <% end %>
    </table>
</div>

这是属于以下情况的控制器(home_page_controller.rb):

class HomePageController < ApplicationController
  def home
    @cities = City.all
  end
end

我已经解决了这个问题已经有两个星期了,并且已经查看了与此类似的其他问题。我已经尝试了所有答案都没有成功。 任何帮助表示赞赏。

此致 Llausa

P.S以下是我在localhost服务器上的外观以及我希望如何在heroku上查看 How I'd like it to look - image here

1 个答案:

答案 0 :(得分:0)

这是因为您使用in_group_of没有选项。用以下代码替换您的代码:

<div class="container-fluid text-center">
    <table>
    <% @cities.in_groups_of(3,false) do |row_cities| %>
        <tr>    
            <% row_cities.each_with_index do |city,index| %>
                <td>
                    <h3><%= city.name %></h3>
                    <p><%= city.country %></p>
                    <%= link_to "See More", city_path(city) %>
                </td>
            <% end %>
        </tr>
    <% end %>
    </table>
</div>

More info here