我知道这里有很多关于类似问题的帖子,但是没有一个对我有帮助。
在VPS上使用Capistrano/passenger
到production mode
部署后,我的应用程序在localhost
上工作得很好,我得到了这个错误。我没有改变代码中的任何东西,控制器或路由…所以我不知道为什么我得到这个错误。
** EDIT **
是否可能发生此错误,因为在VPS
上有ID为1-8的destroyed
类别。
如果我登录到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到....的类别项?
** Update **
当我运行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
这里是rake路由的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类别的Product来排除这种情况。肯定有一种更优雅的方式来编写它,但看起来您现在的数据库中没有太多对象,所以这可能会起作用:
在VCS控制台中:
Product.all.map { |p| {p.id => p.category} }
这将显示产品的id(如果有的话),以及它是指向Category还是指向nil。