轨道中的即时搜索引擎

时间:2015-09-28 07:06:13

标签: ruby-on-rails search-engine partial

我正在尝试在rails中创建即时搜索引擎。 我的可搜索模型是tournament 我将gem 'pg_search'添加到我的gemfile中 在我的锦标赛模型中,我有:

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} }

搜索发生在我的Tournament#index视图中,所以在我的锦标赛控制器中我有:

  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

在我看来(苗条)锦标赛/指数我有:

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"

以下是锦标赛的部分内容:

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'

这是我的index.js.erb文件:

$('#tournament_row').html("<%= render 'tournaments' %>");

当我每次提交表单时检查我的日志时,params [:content]会被发送到我的锦标赛#index动作中,我可以看到 @tournaments = @tournaments.search(params[:content])正在运作。

我的问题是我的锦标赛/索引视图永远不会更新来显示@tournaments中包含的锦标赛...它始终保持不变。有什么问题?

1 个答案:

答案 0 :(得分:2)

尝试添加escape_javascript

$('#tournament_row').html("<%= escape_javascript(render 'tournaments') %>");

$('#tournament_row').html('<%=j render 'tournaments' %>')