簡體   English   中英

ActionView :: Template :: Error(沒有路由與{:action =>“ show”,:controller =>“ categories”,:id => nil}缺少必需的鍵:[:id]):

[英]ActionView::Template::Error (No route matches {:action=>“show”, :controller=>“categories”, :id=>nil} missing required keys: [:id]):

我知道這里有很多關於類似問題的文章,但是沒有一個對我有幫助。

使用Capistrano/passenger將其部署到VPS上的production mode后,我的應用程序在localhost上運行良好,出現此錯誤。 我沒有更改代碼,控制器或路由中的任何內容...所以我不知道為什么會收到此錯誤。

誰能幫我這個???

**編輯**

因為我destroyedVPS上ID為1-8的類別,是否可能發生此錯誤?

如果我登錄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 []>那里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部分

     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上無法正常運行,我完全空白

看起來您仍然具有帶有category_id的Product對象,但也許那個Category對象已經消失了。

我會通過查找類別為nil的產品來排除這種情況。 絕對有一種更優雅的方式來編寫此代碼,但是看來您現在數據庫中沒有太多對象,因此這可能起作用:

在您的VCS控制台中:

Product.all.map { |p| {p.id => p.category} }

這將顯示您的產品ID(如果有),以及它是指向類別還是指向零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM