如何修复Heroku日志中的“((nil:NilClass的未定义方法'子类别')”)错误

时间:2019-07-28 01:58:36

标签: ruby-on-rails ruby heroku

在为我先前的问题How to fix "ActionView::Template::Error" in Heroku/Ruby on Rails实现了解决方案之后,似乎我收到了另一个错误,并且我的rails应用程序仍然无法在Web上显示。这是我的日志:

2019-07-28T02:32:22.916461+00:00 app[web.1]: F, [2019-07-28T02:32:22.916413 #4] FATAL -- : [6fdce32f-7db6-476f-b20e-bb1488d0c88e] ActionView::Template::Error (undefined method `subcategories' for nil:NilClass):
2019-07-28T02:32:22.916632+00:00 app[web.1]: F, [2019-07-28T02:32:22.916564 #4] FATAL -- : [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     32:         </tr>
2019-07-28T02:32:22.916635+00:00 app[web.1]: [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     33:         <tr>
2019-07-28T02:32:22.916637+00:00 app[web.1]: [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     34:           <td>
2019-07-28T02:32:22.916644+00:00 app[web.1]: [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     35:             <% @community.subcategories.each do |subcategory| %>
2019-07-28T02:32:22.916647+00:00 app[web.1]: [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     36:               <ul class="left-list">
2019-07-28T02:32:22.916649+00:00 app[web.1]: [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     37:                 <li><%= link_to subcategory.name, category_subcategory_path(@community, subcategory) %></li>
2019-07-28T02:32:22.916656+00:00 app[web.1]: [6fdce32f-7db6-476f-b20e-bb1488d0c88e]     38:               </ul>
2019-07-28T02:32:22.916662+00:00 app[web.1]: F, [2019-07-28T02:32:22.916613 #4] FATAL -- : [6fdce32f-7db6-476f-b20e-bb1488d0c88e]   
2019-07-28T02:32:22.916713+00:00 app[web.1]: F, [2019-07-28T02:32:22.916661 #4] FATAL -- : [6fdce32f-7db6-476f-b20e-bb1488d0c88e] app/views/categories/index.html.erb:35:in `_app_views_categories_index_html_erb___2746979844282101305_47317257329660'
2019-07-28T02:39:47.392245+00:00 heroku[router]: at=info method=GET path="/" host=damare.herokuapp.com request_id=233346d0-40d0-4a76-84f9-f14676005ecf fwd="71.140.151.125" dyno=web.1 connect=1ms service=46ms status=500 bytes=1827 protocol=https
2019-07-28T02:39:47.352733+00:00 app[web.1]: I, [2019-07-28T02:39:47.352598 #4]  INFO -- : [233346d0-40d0-4a76-84f9-f14676005ecf] Started GET "/" for 71.140.151.125 at 2019-07-28 02:39:47 +0000
2019-07-28T02:39:47.354094+00:00 app[web.1]: I, [2019-07-28T02:39:47.354021 #4]  INFO -- : [233346d0-40d0-4a76-84f9-f14676005ecf] Processing by CategoriesController#index as HTML
2019-07-28T02:39:47.386605+00:00 app[web.1]: D, [2019-07-28T02:39:47.386483 #4] DEBUG -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   [1m[36mCategory Load (1.2ms)[0m  [1m[34mSELECT "categories".* FROM "categories"[0m
2019-07-28T02:39:47.387726+00:00 app[web.1]: I, [2019-07-28T02:39:47.387657 #4]  INFO -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   Rendering categories/index.html.erb within layouts/application
2019-07-28T02:39:47.388892+00:00 app[web.1]: D, [2019-07-28T02:39:47.388812 #4] DEBUG -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   [1m[36mCACHE Category Load (0.0ms)[0m  [1m[34mSELECT "categories".* FROM "categories"[0m
2019-07-28T02:39:47.390941+00:00 app[web.1]: D, [2019-07-28T02:39:47.390863 #4] DEBUG -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   [1m[36mCategory Exists (1.0ms)[0m  [1m[34mSELECT  1 AS one FROM "categories" WHERE "categories"."id" = $1 LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
2019-07-28T02:39:47.393903+00:00 app[web.1]: I, [2019-07-28T02:39:47.393821 #4]  INFO -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   Rendered categories/index.html.erb within layouts/application (6.0ms)
2019-07-28T02:39:47.394112+00:00 app[web.1]: I, [2019-07-28T02:39:47.394047 #4]  INFO -- : [233346d0-40d0-4a76-84f9-f14676005ecf] Completed 500 Internal Server Error in 40ms (ActiveRecord: 15.0ms)
2019-07-28T02:39:47.395066+00:00 app[web.1]: F, [2019-07-28T02:39:47.394952 #4] FATAL -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   
2019-07-28T02:39:47.395117+00:00 app[web.1]: F, [2019-07-28T02:39:47.395024 #4] FATAL -- : [233346d0-40d0-4a76-84f9-f14676005ecf] ActionView::Template::Error (undefined method `subcategories' for nil:NilClass):
2019-07-28T02:39:47.395299+00:00 app[web.1]: F, [2019-07-28T02:39:47.395241 #4] FATAL -- : [233346d0-40d0-4a76-84f9-f14676005ecf]     32:         </tr>
2019-07-28T02:39:47.395303+00:00 app[web.1]: [233346d0-40d0-4a76-84f9-f14676005ecf]     33:         <tr>
2019-07-28T02:39:47.395305+00:00 app[web.1]: [233346d0-40d0-4a76-84f9-f14676005ecf]     34:           <td>
2019-07-28T02:39:47.395307+00:00 app[web.1]: [233346d0-40d0-4a76-84f9-f14676005ecf]     35:             <% @community.subcategories.each do |subcategory| %>
2019-07-28T02:39:47.395309+00:00 app[web.1]: [233346d0-40d0-4a76-84f9-f14676005ecf]     36:               <ul class="left-list">
2019-07-28T02:39:47.395312+00:00 app[web.1]: [233346d0-40d0-4a76-84f9-f14676005ecf]     37:                 <li><%= link_to subcategory.name, category_subcategory_path(@community, subcategory) %></li>
2019-07-28T02:39:47.395314+00:00 app[web.1]: [233346d0-40d0-4a76-84f9-f14676005ecf]     38:               </ul>
2019-07-28T02:39:47.395379+00:00 app[web.1]: F, [2019-07-28T02:39:47.395303 #4] FATAL -- : [233346d0-40d0-4a76-84f9-f14676005ecf]   
2019-07-28T02:39:47.395444+00:00 app[web.1]: F, [2019-07-28T02:39:47.395388 #4] FATAL -- : [233346d0-40d0-4a76-84f9-f14676005ecf] app/views/categories/index.html.erb:35:in `_app_views_categories_index_html_erb___2746979844282101305_47317257329660'
2019-07-28T02:39:47.485739+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=damare.herokuapp.com request_id=3250b493-462c-421f-bdba-33ed78570354 fwd="71.140.151.125" dyno=web.1 connect=1ms service=2ms status=304 bytes=48 protocol=https

我正在尝试使我的Rails应用程序在https://damare.herokuapp.com/上运行 虽然有一个“很抱歉,但出了点问题”消息。该错误似乎是当前的问题:“((nil:NilClass的未定义方法'subcategories')” 这是我的subcategories_controller.rb的代码:

class SubcategoriesController < ApplicationController
  def show
    @listings = Listing.where(subcategory_id: params[:id])
    @category = Category.find(params[:category_id])
    @subcategory = Subcategory.find(params[:id])
  end
  def find_by_category
    category = Category.find(params[:category_id])
    subcategories = category.subcategories.find_all
    render json: {subcategories: subcategories}
  end
end

这是我的category_controller.rb的代码:

class CategoriesController < ApplicationController
  def index
    @categories = Category.all
    @community = @categories[0]
    @housing = @categories[1]
    @jobs = @categories[2]
    @personals = @categories[3]
    @services = @categories[4]
    @for_sale = @categories[5]
  end
  def show
    @listings = Listing.where(category_id: params[:id])
    @category = Category.find(params[:id])
  end
end

这是我的routes.rb的代码:

Rails.application.routes.draw do
  devise_for :users
  resources :categories do
    resources :subcategories
  end
  resources :listings do
    collection do
      get 'search'
    end
  end
  root 'categories#index'
  match '/help', to: 'pages#help', via: :get
  match '/scams', to: 'pages#scams', via: :get
  match '/safety', to: 'pages#safety', via: :get
  match '/terms', to: 'pages#terms', via: :get
  match '/privacy', to: 'pages#privacy', via: :get
  match '/about', to: 'pages#about', via: :get
  match '/contact', to: 'pages#contact', via: :get
  match '/mylistings', to: 'listings#mylistings', via: :get
  match '/subcategoires/find_by_category', to: 'subcategories#find_by_category', via: :post
end

0 个答案:

没有答案