我知道那裏這裏有很多關於類似問題的帖子,但是他們都沒有幫助我。ActionView :: Template :: Error(沒有路由匹配{:action =>「show」,:controller =>「categories」,:id => nil}缺少必需的鍵:[:id]):
我在使用Capistrano/passenger
到production mode
在VPS上部署後,我的應用在localhost
上運行很好我收到此錯誤。我沒有改變代碼,控制器或路由中的任何東西......所以我不知道爲什麼我得到這個錯誤。
任何人都可以幫助我嗎?
**編輯**
是否有可能,這個錯誤發生的事情,因爲我destroyed
類別ID爲1-8的VPS
。
如果我登錄到rails console
這是在category
Category Load (0.6ms) SELECT "categories".* FROM "categories"
=> #<ActiveRecord::Relation [#<Category id: 9, name: "Beauty & Fragrance", created_at: "2016-09-30 10:43:54", updated_at: "2016-10-04 16:35:41">, # <Category id: 10, name: "Jewellery", created_at: "2016-10-04 16:36:40", updated_at: "2016-10-04 16:36:40">, #<Category id: 11, name: "Home Decor", created_at: "2016-10-04 16:37:13", updated_at: "2016-10-04 16:37:13">, # <Category id: 12, name: "Giftwrap and Cards", created_at: "2016-10-04 16:37:42", updated_at: "2016-10-04 16:37:42">]>
中的項目,是有可能的是,在指數的代碼與ID尋找類項目1至....?
** **更新
當我運行Product.where(category_id: [*1..8])
我得到Product Load (0.9ms) SELECT "products".* FROM "products" WHERE "products"."category_id" IN (1, 2, 3, 4, 5, 6, 7, 8) => #<ActiveRecord::Relation []>
所以這是最有可能不是爲什麼這個錯誤是顯示
這是從production.log
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"categories", :id=>nil} missing required keys: [:id]):
23: <% if index == 0 %>
24: <div class="col-lg-4 col-sm-6 col-xs-12 center-block " >
25:
26: <%= link_to category_path (category) do %>
27: <%= image_tag product.image.url(:medium), class: "img-responsive" %>
28: <% end %>
29: <div class="caption">
app/views/pages/index.html.erb:26:in `block (3 levels) in _app_views_pages_index_html_erb___619502042981659248_47242510413860'
app/views/pages/index.html.erb:22:in `each'
app/views/pages/index.html.erb:22:in `each_with_index'
app/views/pages/index.html.erb:22:in `block (2 levels) in _app_views_pages_index_html_erb___619502042981659248_47242510413860'
app/views/pages/index.html.erb:20:in `each'
app/views/pages/index.html.erb:20:in `block in _app_views_pages_index_html_erb___619502042981659248_47242510413860'
app/views/pages/index.html.erb:18:in `each'
app/views/pages/index.html.erb:18:in `each_slice'
app/views/pages/index.html.erb:18:in `_app_views_pages_index_html_erb___619502042981659248_47242510413860'
這裏是來自`pages_controller.rb的索引方法
def index
@products = Product.all.order(created_at: :desc).group_by(&:category_id)
@images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg"]
@random_no = rand(10)
@random_image = @images[@random_no]
end
這裏是categories_controller.rb
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
def index
@categories = Category.all
end
def show
@products = @category.products
@images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"]
@random_no = rand(5)
@random_image = @images[@random_no]
end
private
def set_category
@category = Category.includes(:products).find(params[:id])
end
def category_params
params.require(:category).permit(:name)
end
end
這裏是pages/index.html.erb
<div class="container-fluid">
<% @products.each_slice(3) do |products_group| %>
<div class="row">
<% products_group.each do |category, products| %>
<% products.each_with_index do |product, index| %>
<% if index == 0 %>
<div class="col-lg-4 col-sm-6 col-xs-12 center-block " >
<%= link_to category_path (category) do %>
<%= image_tag product.image.url(:medium), class: "img-responsive" %>
<% end %>
<div class="caption">
<p class="category-name" ><%= product.category.name %></p>
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
這裏是config/routes.rb
Rails.application.routes.draw do
get 'products/search' => 'products#search', as: 'search_products'
post 'emaillist/subscribe' => 'emaillist#subscribe'
resources :categories
resources :labels
resources :products do
resources :product_items
end
resources :carts
resources :orders
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
resources :contacts, only: [:new, :create]
root 'pages#index'
get 'about' => 'pages#about'
get 'location' => 'pages#location'
get 'help' => 'pages#help'
end
這裏是耙潰敗的category
部分ES
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
正如我說,這之前在本地主機的正常使用,我完全空白的,爲什麼它不工作相同的VPS
您是否已經遷移了數據庫? –
是的,它在使用Capistrano部署時發生automaticaly – Slowboy
我覺得'<%= link_to category_path(category)do%>'should%'<%= link_to category_path(category.id)do%>' –