繁体   English   中英

使用 searchkick 进行的 ROR 搜索不起作用

[英]ROR search for with searchkick is not working

我一直在做一个网页项目,人们可以在其中发布和搜索优惠。 我对这个真的很陌生,我一直在研究如何制作一个简单的搜索表单。 我目前正在使用 searchkick gem 并且我已经学习了很多教程,但是我的代码似乎不起作用。 先感谢您

这是我的相关代码

    #app/controllers/offers_controller.rb
    class OffersController < ApplicationController
        before_action :authenticate_user!
        before_action :set_offer, only: [:show, :edit, :update, :destroy]

       def index
       search = params[:term].present? ? params[:term] : nil
       @offers = if search
               Offer.search(search)
           else
              Offer.all
           end
       end


#db/migrate/create_offers.rb
class CreateOffers < ActiveRecord::Migration[5.1]
   def change
      create_table :offers do |t|
         t.string :nombre
         t.text :descripcion
         t.string :imagen, null:true
         t.references :user, foreign_key: true
         t.timestamps
      end
   end
end

 #app/models/offer.rb
class Offer < ApplicationRecord
   searchkick word_start: [:nombre] # word_middle: [:nombre, :descripcion]


  def search_data
   { 
     nombre: nombre,
     descripcion: descripcion
   }
  end

 #app/views/layouts/_header.html.erb
  <div class="input-group-btn search-panel">
            <%= submit_tag 'Search', name: nil, class: "btn btn-default" %>
  </div> 

从 searchkick 文档中,试试这个

Offer.search search, fields: [:nombre], match: :word_start

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM