我已经跑步:
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
这是我的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
这是我的route.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/
在我的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')
子类别存储在社区类别中。这些子类别也在我的seed.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)
答案 0 :(得分:0)
您这样声明@community
变量:
@categories = Category.all
@community = @categories[0]
因此,当数据库中没有类别时,@community
是nil
。
这会在您调用app/views/categories/index.html.erb:29
的{{1}}中引起问题。由于category_path(@community)
为@community
,因此其值为nil
。提示错误。
在显示此category_path(nil)
之前,您应该检查@community
是否存在。