问题描述
我已经跑步:
git add .
git commit -m "Some helpful message for your future self"
在我的终端上.我也将Rails应用程序推送到Heroku上,然后运行:
in my terminal. I have also pushed my rails application onto Heroku, and I've ran:
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
这是我的category_controller.rb文件:
This is my categories_controller.rb file:
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
这是我的route.rb文件:
This is my routes.rb file:
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/
在我的seed.rb文件中创建了类别:
The categories are created in my seeds.rb file:
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文件中创建.以下是社区类别中的子类别:
Subcategories are stored in the community category. The subcategories are also created in my seeds.rb file. 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)
推荐答案
您可以这样声明@community
变量:
@categories = Category.all
@community = @categories[0]
因此,当数据库中没有类别时,@community
是nil
.
So when there are no categories in your database @community
is nil
.
这会在您调用category_path(@community)
的app/views/categories/index.html.erb:29
中引起问题.由于@community
为nil
,因此其值为category_path(nil)
.提示错误.
Which causes a problem in app/views/categories/index.html.erb:29
where you call category_path(@community)
. Since @community
is nil
it evaluates to category_path(nil)
. Hense the error.
在显示此link
之前,应检查是否存在@community
.
You should check if @community
is present before showing this link
.
这篇关于如何修复"ActionView :: Template :: Error"在Heroku/Ruby on Rails中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!