I am trying to create an instant search engine in rails.
My searchable model is tournament
I added the gem 'pg_search'
to my gemfile
In my tournament model I have :
class Tournament < ActiveRecord::Base
include PgSearch
pg_search_scope :search,
:against => [:name, :address, :city, :club_organisateur, :starts_on, :ends_on, :postcode],
:using => {:tsearch => {:prefix => true} }
The search takes place on my Tournament#index
view, so in my tournaments controller I have :
def index
@tournaments = policy_scope(Tournament)
if @tournaments.blank? && current_user.judge?
render 'pages/partials/_no_tournaments_judge'
elsif @tournaments.blank?
render 'pages/partials/_no_tournaments'
elsif params[:content].blank?
elsif params[:content]
@tournaments = @tournaments.search(params[:content])
respond_to do |format|
format.js {}
format.html{ render :index }
end
end
end
In my view (slim) tournament/index I have :
div class="container"
div class="row"
div class="col-xs-12"
h1 class="emperor" Recherche de tournois
= form_tag tournaments_path, method: "get", :remote => true do ||
div class="form-group"
= label_tag :content, "recherche"
= text_field_tag :content, nil, class: "form-control"
= render "tournaments"
Here is the tournaments' partial :
div class="row" id="tournament_row"
- @tournaments.each do |tournament|
div class="col-md-4"
div class="panel panel-default"
div class="panel-heading"
h2
= tournament.name
br
small = tournament.city.upcase
div class="panel-body"
p
'Du
strong> = tournament.starts_on.strftime("%d/%m/%Y")
'au
strong = tournament.ends_on.strftime("%d/%m/%Y")
ul
- tournament.competitions.each do |competition|
li
= "#{competition.category} #{competition.genre.text}"
div class="panel-footer"
= link_to 'En savoir plus', tournament_path(tournament), class: 'btn btn-primary'
and Here is my index.js.erb file :
$('#tournament_row').html("<%= render 'tournaments' %>");
When I check my logs everytime I submit the form, params[:content] is sent to my tournament#index action and I can see that
@tournaments = @tournaments.search(params[:content])
is working.
My problem is that my tournament/index view is never updating to display the tournaments contained in @tournaments... It always stay the same. What is the problem ?
Try to add escape_javascript
$('#tournament_row').html("<%= escape_javascript(render 'tournaments') %>");
or
$('#tournament_row').html('<%=j render 'tournaments' %>')