Slowboy Slowboy - 2 months ago 6
Ruby Question

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

I know there are lots of posts here about similar issues, but none of them are helping me.

My app is working great on

after deploying it using
production mode
on a VPS I'm getting this error. I haven't changed anything in the code, controllers or routes... so I'm without a clue why I'm getting this error.

Can anyone help me with this???

** EDIT **

Is it possible that this error is happening because I
categories with ID 1-8 on the

If I log into
rails console
this is the items in the

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">]>

So is it possible that the code in the index is looking for category item with ID from 1 to ....?

** Update **

when I run
Product.where(category_id: [*1..8])
I get
Product Load (0.9ms) SELECT "products".* FROM "products" WHERE "products"."category_id" IN (1, 2, 3, 4, 5, 6, 7, 8) => #<ActiveRecord::Relation []>

So that is most likely not why this error is showing

this is from the

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 " >
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'

Here is the index method from the `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]

Here is the

class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
def index
@categories = Category.all

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]

def set_category
@category = Category.includes(:products).find(params[:id])

def category_params


Here is the

<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" ><%= %></p>
<% end %>
<% end %>
<% end %>
<% end %>


and here is the

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

resources :carts

resources :orders

devise_for :admin_users, ActiveAdmin::Devise.config

resources :contacts, only: [:new, :create]

root 'pages#index'

get 'about' => 'pages#about'

get 'location' => 'pages#location'

get 'help' => 'pages#help'


here is the
part of the rake routes

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

As I said before this is working perfectly in the localhost and I'm totally blank about why it's not working the same on the VPS


It still looks like you have a Product objects with a category_id, but maybe that Category object is gone.

I'd rule that out by looking for a Product that has a nil category. There is definitely a more elegant way to write this, but it looks like you don't have too many objects in your database right now, so this might work:

In your VCS console: { |p| { => p.category} }

That will show you the Product's id, if any, and whether it points to a Category or to nil.