pg_search没有处理简单示例

时间:2017-06-08 23:08:49

标签: ruby-on-rails postgresql pg-search

我正在尝试使用gem" pg_search"它不起作用。

class ShopItem < ApplicationRecord
    has_many :cart_items
    has_attached_file :shop_image, styles: { medium: "300x300>", large: "500x500>", very_large: "600x600>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :shop_image, content_type: /\Aimage\/.*\z/
    include PgSearch
    pg_search_scope :search_by_text, against: [:title1, :description1, :title2, :description2]
end

我的控制器

class ShopItemsController < ApplicationController

  def index

    if params[:query].present?
      @shop_items = ShopItem.search_by_text(params[:query]).paginate(:page => params[:page], :per_page => 32).order('created_at DESC')
    else
      @shop_items = ShopItem.all.paginate(:page => params[:page], :per_page => 32).order('created_at DESC')
    end

  end
end

在我的shop_items_path中,我有一个像

这样的搜索字段
<%= form_tag shop_items_path, :method => 'get'  do %>
      <%= text_field_tag :search, params[:query], autofocus: true,  type: "text", placeholder: 'search for messages'%>
      <%= submit_tag "search", type: "submit"%>
      <% end %> 

当我使用搜索时,我在浏览器查询中有:http://localhost:3000/shop_items?utf8=%E2%9C%93&search=first+shop+item&commit=search

来自控制台的一些数据:

Started GET "/shop_items?utf8=%E2%9C%93&search=first&commit=search" for 127.0.0.1 at 2017-06-09 03:05:42 -0400
Processing by ShopItemsController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>"first", "commit"=>"search"}
  Contact Load (0.5ms)  SELECT  "contacts".* FROM "contacts" ORDER BY "contacts"."id" DESC LIMIT $1  [["LIMIT", 1]]
  ShoppingCart Load (0.3ms)  SELECT  "shopping_carts".* FROM "shopping_carts" WHERE "shopping_carts"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering shop_items/index.html.erb within layouts/application
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.4ms)  SELECT COUNT(*) FROM "shop_items"
  Rendered shop_items/index.html.erb within layouts/application (7.4ms)
  Rendered layouts/_header.html.erb (6.5ms)
  Rendered layouts/_footer.html.erb (1.1ms)
Completed 200 OK in 125ms (Views: 120.6ms | ActiveRecord: 1.5ms)

没有得到。 但如果我使用的是rails console:ShopItem.search_by_text("item") 它给我一个对象

出了什么问题?

0 个答案:

没有答案