ruby-on-rails - 如何修复 Heroku/Ruby on Rails 中的“ActionView::Template::Error”
问题描述
我跑了:
git add .
git commit -m "Some helpful message for your future self"
在我的终端。我还将我的 rails 应用程序推送到 Heroku 上,并且运行了:
heroku run rails db:migrate
我的应用程序显示“我们很抱歉,但出了点问题”。虽然错误。这是我的日志中显示的内容:
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
这是我的 categories_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
页面网址: https ://damare.herokuapp.com/
这些类别是在我的seeds.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')
子类别存储在社区类别中。子类别也在我的seeds.rb 文件中创建。以下是社区类别中的子类别:
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)
解决方案
你这样声明@community
变量:
@categories = Category.all
@community = @categories[0]
因此,当您的数据库中没有类别时@community
是nil
.
这会导致app/views/categories/index.html.erb:29
您调用的位置出现问题category_path(@community)
。因为@community
isnil
它评估为category_path(nil)
. Hense 错误。
在显示此之前,您应该检查是否@community
存在link
。
推荐阅读
- python - 为什么 {} 可以使用整数而 set() 不能?
- python - 删除python中文本文件的下划线和连字符以外的所有符号
- javascript - 如何在参数列表后修复“未捕获的语法错误:缺少)”
- android - Android Room如何自动生成序列计数
- c# - 当通过 TypeBuilder.CreateType 实现一个带有 'in' 参数的方法的接口时,会抛出 TypeLoadException
- tags - DICOM 标头中的 Time (0040,A122) 标签是什么意思?
- android - 无法通过参数传递深层链接 - Android
- javascript - Javascript:查找唯一字符的最长子串的长度
- typescript - 如何导入从模块导出的类?
- javascript - nouislider 步骤不适用于多个范围