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

时间:2016-10-04 18:36:33

标签: ruby-on-rails ruby routes vps

我知道这里有很多关于类似问题的帖子,但他们都没有帮助我。

我的应用在使用localhostCapistrano/passenger部署到VPS后production mode上运行得很好我发现此错误。我没有更改代码,控制器或路由中的任何内容......所以我不知道为什么我会收到此错误。

任何人都可以帮我这个???

**编辑**

是否可能发生此错误,因为destroyed上的VPS类别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 []>

因此很可能不是为什么会出现此错误

来自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上不起作用

1 个答案:

答案 0 :(得分:1)

看起来你有一个带有category_id的Product对象,但是这个Category对象可能已经消失了。

我通过寻找具有零类别的产品来排除这种情况。绝对有一种更优雅的方式来编写它,但看起来你现在没有太多的对象在你的数据库中,所以这可能有用:

在VCS控制台中:

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

这将显示产品的ID(如果有),以及它是指向类别还是零。