简体   繁体   English

如何在Heroku / Ruby on Rails中修复“ ActionView :: Template :: Error”

[英]How to fix “ActionView::Template::Error” in Heroku/Ruby on Rails

I have run: 我跑了:

git add .
git commit -m "Some helpful message for your future self"

in my terminal. 在我的终端上。 I have also pushed my rails application onto Heroku, and I've ran: 我也将Rails应用程序推送到Heroku上,然后运行:

heroku run rails db:migrate

My app shows the "We're sorry, but something went wrong." 我的应用程序显示“很抱歉,出了点问题。” error though. 错误。 This is what shows up in my logs: 这是我的日志中显示的内容:

2019-07-28T00:08:13.687554+00:00 app[web.1]: F, [2019-07-28T00:08:13.687495 #4] FATAL -- : [b4dfe4a3-136d-499a-83f1-be0d4e82fb47] ActionView::Template::Error (No route matches {:action=>"show", :controller=>"categories", :id=>nil}, missing required keys: [:id]):
2019-07-28T00:08:13.687702+00:00 app[web.1]: F, [2019-07-28T00:08:13.687649 #4] FATAL -- : [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     26:     <div class="center-one">
2019-07-28T00:08:13.687705+00:00 app[web.1]: [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     27:       <table>
2019-07-28T00:08:13.687706+00:00 app[web.1]: [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     28:         <tr>
2019-07-28T00:08:13.687708+00:00 app[web.1]: [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     29:           <th class="table-header"><%= link_to 'community', category_path(@community) %></th>
2019-07-28T00:08:13.687710+00:00 app[web.1]: [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     30:         </tr>
2019-07-28T00:08:13.687711+00:00 app[web.1]: [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     31:         <tr>
2019-07-28T00:08:13.687713+00:00 app[web.1]: [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]     32:           <td>
2019-07-28T00:08:13.687741+00:00 app[web.1]: F, [2019-07-28T00:08:13.687697 #4] FATAL -- : [b4dfe4a3-136d-499a-83f1-be0d4e82fb47]   
2019-07-28T00:08:13.687806+00:00 app[web.1]: F, [2019-07-28T00:08:13.687747 #4] FATAL -- : [b4dfe4a3-136d-499a-83f1-be0d4e82fb47] app/views/categories/index.html.erb:29:in `_app_views_categories_index_html_erb___3528218001569079456_46996040052660'
2019-07-28T00:08:14.760735+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=damare.herokuapp.com request_id=78e904dc-8e9b-4849-ba3e-a137dd23ba60 fwd="71.140.151.125" dyno=web.1 connect=0ms service=1ms status=304 bytes=48 protocol=https

This is my categories_controller.rb file: 这是我的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

This is my routes.rb file: 这是我的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

Url of the page: https://damare.herokuapp.com/ 页面网址https//damare.herokuapp.com/

The categories are created in my seeds.rb file: 类别在我的seed.rb文件中创建:

community_category = Category.where(name: 'community').first_or_create(name: 'community')
housing_category = Category.where(name: 'housing').first_or_create(name: 'housing')
jobs_category = Category.where(name: 'jobs').first_or_create(name: 'jobs')
personals_category = Category.where(name: 'personals').first_or_create(name: 'personals')
services_category = Category.where(name: 'services').first_or_create(name: 'services')
for_sale_category = Category.where(name: 'for_sale').first_or_create(name: 'for sale')

Subcategories are stored in the community category. 子类别存储在社区类别中。 The subcategories are also created in my seeds.rb file. 这些子类别也在我的seed.rb文件中创建。 Here are the subcategories in the community category: 以下是社区类别中的子类别:

Subcategory.where(name: 'activities', category_id: community_category.id).first_or_create(name: 'activities', category_id: community_category.id)
Subcategory.where(name: 'classes', category_id: community_category.id).first_or_create(name: 'classes', category_id: community_category.id)
Subcategory.where(name: 'events', category_id: community_category.id).first_or_create(name: 'events', category_id: community_category.id)
Subcategory.where(name: 'general', category_id: community_category.id).first_or_create(name: 'general', category_id: community_category.id)
Subcategory.where(name: 'new', category_id: community_category.id).first_or_create(name: 'news', category_id: community_category.id)
Subcategory.where(name: 'musicians', category_id: community_category.id).first_or_create(name: 'musicians', category_id: community_category.id)
Subcategory.where(name: 'pets', category_id: community_category.id).first_or_create(name: 'pets', category_id: community_category.id)

You declare @community variable like this: 您可以这样声明@community变量:

@categories = Category.all
@community = @categories[0]

So when there are no categories in your database @community is nil . 因此,当数据库中没有类别时, @communitynil

Which causes a problem in app/views/categories/index.html.erb:29 where you call category_path(@community) . 这会导致app/views/categories/index.html.erb:29 ,您在其中调用category_path(@community) Since @community is nil it evaluates to category_path(nil) . 由于@communitynil因此其计算结果为category_path(nil) Hense the error. 提示错误。

You should check if @community is present before showing this link . 在显示此link之前,您应该检查@community是否存在。

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

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