[英]ROR search for with searchkick is not working
I've been working on a web page project in which people post and search for offers.我一直在做一个网页项目,人们可以在其中发布和搜索优惠。 I'm truly new to this and I've been researching about how to make a simple search form.我对这个真的很陌生,我一直在研究如何制作一个简单的搜索表单。 I'm currently working with searchkick gem and i've follow lot of tutorial, however my code doesn't seem to work.我目前正在使用 searchkick gem 并且我已经学习了很多教程,但是我的代码似乎不起作用。 Thank you in advance先感谢您
Here is my relevant code这是我的相关代码
#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.