-1

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> 
  • What is your question? – sawa Oct 20 '17 at 04:19
  • Why is it not working? – Mariana Ramírez Oct 20 '17 at 20:32
  • Is there an error that is occurring? If so, share it. Have you tried running `Offer.search("your search terms")` in an irb console session? If you see the correct results then you can rule out that line of code. Is your search term being passed through the params to params[:term] correctly? I would print it to the logs to make sure. – BigRon Nov 10 '17 at 14:53

1 Answers1

0

From the searchkick doc, try this

Offer.search search, fields: [:nombre], match: :word_start
Dias
  • 862
  • 8
  • 17