【问题标题】:Using Ajax to return Sunspot search results使用 Ajax 返回 Sunspot 搜索结果
【发布时间】:2014-08-21 21:49:39
【问题描述】:

我的目标是让我的搜索表单在搜索后自动更新,而无需重新加载页面。我在我的 Rails 应用程序中使用 Sunspot gem。有什么想法吗?

这是我的搜索表单 search.html.haml

    - if current_user
     = link_to "Post New Job", new_job_path, class: "btn btn-primary"

    .pull-right
      =form_tag jobs_path, method:  :get, class: "form-search", id: "job-search" do
      %p
       =text_field_tag :search, params[:search], placeholder: "Job Listings", class: "input-medium search-query"
       =submit_tag "Search", :name => nil, class: "btn btn-default"

    .jobs
     =render 'jobs'

这是渲染的作业表

    %table.table.table-striped
     %thead
      %tr
       %th Title
       %th Posted By
       %th Active Until

    %tbody
     - @jobs.each do |job|
      %tr
       %td= link_to job.title, job
       %td= job.author.email
       %td= job.end_date

这是我在 application.js 中使用的 Jquery

    $(function () {

     $('#job-search').submit(function () {
      $.get(this.action, $(this).serialize(), null, 'script');
     return false;
     });
    });

我的控制器看起来像这样

      def index
       @jobs = Job.active

       @search = @jobs.search do
        fulltext params[:search]
       end
       @jobs = @search.results
      end

即使在我输入搜索参数后将 remote 设置为 true,结果也不会显示。我正在使用“jquery-rails”gem。

【问题讨论】:

    标签: jquery ruby-on-rails ajax sunspot


    【解决方案1】:

    我通过在名为 search.js.erb 的文件中添加这行代码来修复它

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多