I am building a small classifieds application for our community where folks can list products for sale using a form with params description and image only.
Both params are in the schema.rb
THE PROBLEM:
I have embedded the search bar on my index.html.erb and also created a show.html.erb which is blank. Currently the search bar does nothing except look pretty,
I am struggling to understand how to get the search wired up to interrogate the description param and deliver the results onto my show.html.erb.
NB: I would like the show.html.erb page and its search results to mirror my index page cosmetically and achieve this task without having to install any further gems.
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 15)
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render :new
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render :edit
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find_by(id: params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
Change your index method to either have a search query or not.
def index
if params[:query]
@results = Advert.where('lower(description) LIKE ?', '%#{params[:query].downcase}%')
else
@results = Advert.all
end
end
You can also create filters using javascript or install either searchkick
gem or if you need something REALLY simple install rails4-autocomplete
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.