我有一个奇怪的问题,我网站上每个包含数据库查询的页面都会加载一些额外的项目。我以前从未遇到过此问题,试图检查所有控制器,但没有发现任何问题。这是我加载(或刷新)主页时的输出示例:
=> Booting Puma
=> Rails 5.1.6 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/c/wines" for 127.0.0.1 at 2018-11-02 15:55:30 -0400 (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by CatalogsController#show as HTML
Parameters: {"id"=>"wines"}
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."slug" = ? LIMIT ? [["slug", "wines"], ["LIMIT", 1]]
Rendering catalogs/show.html.haml within layouts/application (0.2ms) SELECT COUNT(*) FROM "categories" WHERE "categories"."catalog_id" = ? [["catalog_id", 1]] (0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Category Exists (0.3ms) SELECT 1 AS one FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? LIMIT ? [["open", "t"], ["LIMIT", 1]]
Category Load (0.9ms) SELECT "categories".* FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? [["open", "t"]]
CACHE Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? [["open", "t"]]
CACHE Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? [["open", "t"]]
Product Load (0.8ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 AND "products"."open" = ? AND "products"."category_id" = 1 [["open", "t"]]
Catalog Load (0.7ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 AND "products"."open" = ? AND "products"."category_id" = 2 [["open", "t"]]
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Rendered catalogs/show.html.haml within layouts/application (142.7ms)
Rendered application/_favicon.html.haml (11.1ms)
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (23.7ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (15.7ms)
Completed 200 OK in 937ms (Views: 890.8ms | ActiveRecord: 9.4ms)
Started GET "/uploads/catalog/photo/1/hero-8.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Processing by StaticController#home as HTML
Started GET "/uploads/product/photo/4/pc_pistachio.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/uploads/product/photo/7/pc_pistachio.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/uploads/product/photo/1/pc_creame.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/uploads/product/photo/10/pc_pistachio.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Rendering static/home.html.haml within layouts/application
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.4ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.9ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (95.0ms)
Rendered application/_favicon.html.haml (15.0ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (19.9ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (18.0ms)
Completed 200 OK in 400ms (Views: 389.1ms | ActiveRecord: 4.3ms)
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Processing by StaticController#home as HTML
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Processing by StaticController#home as HTML
Rendering static/home.html.haml within layouts/application
Processing by StaticController#home as HTML
Catalog Load (0.5ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
Rendering static/home.html.haml within layouts/application
Processing by StaticController#home as HTML
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
Rendering static/home.html.haml within layouts/application
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.1ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Rendering static/home.html.haml within layouts/application
(0.4ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
(0.1ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Product Exists (0.4ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Product Exists (0.4ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Product Load (0.6ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (206.4ms)
Rendered application/_favicon.html.haml (6.5ms)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (195.5ms)
Rendered application/_favicon.html.haml (0.1ms)
Rendered static/home.html.haml within layouts/application (246.3ms)
Rendered static/home.html.haml within layouts/application (255.3ms)
Rendered application/_favicon.html.haml (0.1ms)
Rendered application/_favicon.html.haml (0.0ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (13.9ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (9.7ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (3.2ms)
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (3.2ms)
Rendered shared/_navbar.html.haml (14.3ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (5.9ms)
Rendered shared/_footer.html.haml (3.0ms)
Rendered shared/_footer.html.haml (6.0ms)
Completed 200 OK in 859ms (Views: 853.1ms | ActiveRecord: 3.5ms)
Completed 200 OK in 802ms (Views: 795.7ms | ActiveRecord: 4.0ms)
Completed 200 OK in 849ms (Views: 842.5ms | ActiveRecord: 3.7ms)
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:32 -0400
Processing by StaticController#home as HTML
Completed 200 OK in 858ms (Views: 851.2ms | ActiveRecord: 3.8ms)
Rendering static/home.html.haml within layouts/application
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.3ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.6ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.5ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.5ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (58.7ms)
Rendered application/_favicon.html.haml (9.7ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (16.9ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (11.7ms)
Completed 200 OK in 261ms (Views: 254.4ms | ActiveRecord: 4.7ms)
我的静态控制器:
class StaticController < ApplicationController
def home
@catalogs = Catalog.all.where(open: true).order('created_at ASC').limit(6)
# @products = Product.all.includes(:catalog, :category).where(open: true).order('created_at DESC').take(8)
end
def about
@catalogs = Catalog.all.where(open: true)
@categories = Category.all.where(open: true)
@products = Product.all.where(open: true)
end
def download_pdf
send_file "#{Rails.root}/app/assets/docs/1_s.pdf", type: "application/pdf", x_sendfile: true
end
end
注意:我在其他控制器上也遇到类似的问题
一开始,我认为这是一个包,因为我没有添加任何新代码。请让我知道是否需要控制器提供的更多代码。非常感谢您的时间和帮助。
更新
应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :prepare_meta_tags, if: "request.get?"
def prepare_meta_tags(options={})
site_name = "Alvian Imports Inc."
title = "Alvain Imports Inc."
description = "For many years, Alvian imports alcohol accross Florida state and now it's time to grow. Absolutly new team with new products that will be loved by everyone. We have an exclusive partnership with Ponche Caribe and we are starting to deliver our products to Texas, California, Mexico, and Columbia. Our goal is to make alcohol importation better and faster, so our team is working hard to deliver wonderful products to your closest stores."
image = options[:image] || "app/assets/images/brand/logo.png"
current_url = request.url
# Let's prepare a nice set of defaults
defaults = {
site: site_name,
title: title,
image: image,
description: description,
keywords: %w[food food processor food service food and drink f&b food industry food and beverage service food & beverage food processing industry food and beverage manager food and beverage industry food service jobs food beverage beverage industry food service companies f&b service food service worker food industry jobs food service industry food & drink food drink food and beverage management types of food service f&b industry food and beverage department import imports alvian alvianimports miami usa florida],
twitter: {
site_name: site_name,
site: '@IncAlvian',
card: 'The Beverage Company. Ponche Caribe Curacao Blue & Liquors Exclusive Distributor Florida - Texas - California - Colombia Distributor. Must be 21 +',
description: description,
image: image
},
og: {
url: current_url,
site_name: site_name,
title: title,
image: image,
description: description,
type: 'website'
}
}
options.reverse_merge!(defaults)
set_meta_tags options
end
end
Application.html.haml代码:
!!!
%html(lang="en-US" class="region--#{controller.controller_name}")
%head
= display_meta_tags
%meta(http-equiv="X-UA-Compatible" content="IE=edge,chrome=1")
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%meta{:charset => "utf-8"}/
%meta{:content => "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no", :name => "viewport"}/
%meta{:content => "user-scalable=no, width=device-width, initial-scale=1.0", :name => "viewport"}/
%meta{:content => "yes", :name => "apple-mobile-web-app-capable"}/
%meta{:'http-equiv' => "X-UA-Compatible", :content => "IE=edge"}
%meta{:name => "HandheldFriendly", :contnet => "true"}
%meta{:name => "MobileOptimized", :content => "176"}
= render 'application/favicon'
%title #{content_for?(:title) ? yield(:title) : 'Alvian Imports'}
= csrf_meta_tags
= stylesheet_link_tag 'application', media: 'all'
= analytics_init if GoogleAnalytics.valid_tracker?
%body(class = '#{controller.controller_name}')
%div(class="application-wrapper")
= render partial: 'shared/navbar'
= content_tag :main, class: "layout layout-#{controller.action_name}" do
= yield
= render partial: 'shared/footer'
- unless params[:nojs]
= javascript_include_tag 'application'
更新2
我有3种型号:目录,类别和产品。
目录模型:
class Catalog < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
mount_uploader :photo, PhotoUploader
has_many :categories, dependent: :delete_all
has_many :products, :through => :categories, :source => :catalog, dependent: :delete_all
# Important fields
validates_presence_of :name
end
类别模型:
class Category < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
belongs_to :catalog, :foreign_key => 'catalog_id'
has_many :products, dependent: :delete_all
# Important fields
validates_presence_of :name
def self.active
where(status: 1)
end
end
产品型号:
class Product < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
mount_uploader :photo, PhotoUploader
belongs_to :catalog, :foreign_key => 'catalog_id'
belongs_to :category, :foreign_key => 'category_id'
# Important fields
validates_presence_of :name
end
控制台中的预期输出:
Started GET "/" for 127.0.0.1 at 2018-11-02 16:41:26 -0400
Processing by StaticController#home as HTML
Rendering static/home.html.haml within layouts/application
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.1ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.3ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (44.4ms)
Rendered application/_favicon.html.haml (7.9ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (15.8ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (8.9ms)
Completed 200 OK in 218ms (Views: 213.2ms | ActiveRecord: 3.1ms)
主页HTML(HAML),但是必须提及的是,我在其他需要查询的页面(例如目录显示页面)上也遇到了类似的问题:
= content_for :navbar_class, "navbar-dark"
%section(class="hero hero-homepage")
%div(class="hh-container")
%div(class="hh-carousel")
- @catalogs.each do |catalog|
%div(class="hhc-item")
= link_to catalog, class: 'hhc-container' do
%div(class="hhc-background" style="background-image: url(#{catalog.photo});")
-# = image_tag(catalog.photo, lazy: true, class: "hhc-background")
%div(class="hhc-caption")
%h4(class="heading heading-1 is-light") #{catalog.name}
- if !catalog.description.empty?
%p(class="text text-2 is-light is-transparent") #{truncate(catalog.description, :length => 112)}
%section(class="section")
- @catalogs.each do |pc|
- @products = Product.all.where(catalog_id: pc)
%div(class="content")
%div(class="header header-hr")
%span
%h4(class="heading heading-4") New #{pc.name}
- if @products.count > 6
%span
= link_to "View all " + @products.count.to_s, pc, class: "link link-secondary link-arrow"
%div(class="row")
- if !@products.empty?
- @products.includes(:catalog, :category).take(6).each do |p|
%div(class="col-xs-6 col-md-4 col-lg-3")
= link_to catalog_category_product_url(p.catalog, p.category, p), class: "block block-product" do
= image_tag p.photo, alt: p.name, class: 'bp-photo', lazy: true
%div(class="bp-desc")
%span
%h5(class="heading heading-5") #{p.name}
%span
%p(class="text text-3")
%span.is-primary #{p.catalog.name}
%span - #{p.category.name}
- else
%div(class="col-xs-12 is-centered")
%h5(class="heading heading-5") Currently this list is empty.
更新3
好的,我不了解问题的过程,但是我决定重新创建数据库:
rake db:drop db:create db:migrate
解决了问题。
答案 0 :(得分:2)
一堆东西,如果不实际运行代码就很难提供帮助。
首先,使自己熟悉命名方面的良好做法。
@catalogs.each do |pc|
是禁忌,应该是@catalogs.each do |catalog|
。
if !@products.empty?
和@product.count
,它们触发两个查询,我的建议是将计数写入@products_count
并替换为空?与@product_count > 0
在模型中使用scope
来代替.where(open: true)
在@products.includes(:catalog, :category)
中,您可以删除catalog
,因为您已经按目录浏览了产品,并且可以使用pc
(或我在上面推荐的catalog
)代替{ {1}}
如果product.catalog
仅使用ID(例如catalog_category_product_url
),则可以使用catalog/5/category/2/product/23
来防止一些额外的查询(特别是如果您忘记使用eager的话)加载或不想从数据库加载大块数据。