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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.