【发布时间】:2017-02-13 00:59:53
【问题描述】:
我知道这里有很多关于类似问题的帖子,但没有一个对我有帮助。
我的应用在 VPS 上使用 Capistrano/passenger 部署到 production mode 后在 localhost 上运行良好,但出现此错误。我没有更改代码、控制器或路由中的任何内容...所以我不知道为什么会收到此错误。
谁能帮我解决这个问题???
** 编辑 **
是否有可能发生此错误,因为我在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到...的类别项目?
** 更新**
当我运行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 路由的categorypart
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 上运行不一样
【问题讨论】:
-
你迁移数据库了吗?
-
是的,它在 Deploy with Capistrano 中自动发生
-
我认为
<%= link_to category_path (category) do %>应该是<%= link_to category_path (category.id) do %> -
好的,@SantoshSharma 我在
localhost上试过了,但它在那里出错,你认为它可以在 VPS 上工作吗? -
因为你破坏了分类。在您的 VPS 控制台上,查看您是否有任何没有相应类别的挥之不去的产品。如果是这样,您需要清除它们。 Product.where(category_id: [*1..8]) 将显示它们。
标签: ruby-on-rails ruby routes vps