【问题标题】:How should I use jQuery with Sunspot in Rails?我应该如何在 Rails 中使用 jQuery 和 Sunspot?
【发布时间】:2013-08-02 06:55:18
【问题描述】:

我只想在同一页面中显示搜索结果,而不重定向到默认搜索链接,如“.....search=&commit=Search”。我的初衷是使用 jQuery 来呈现与搜索结果相同的表单。 并且下面设置了远程选项

>   <%= form_tag index_by_dvp_path,:method => :get do %>
>        <p>
>           <%= text_field_tag :search, params[:search] %>
>           <%= submit_tag "Search",remote: true %>
>        </p>
>        <% end %>

但是当我点击搜索按钮时,页面仍然会重定向到我提到的默认链接。

我不确定问题出在哪里。

【问题讨论】:

    标签: jquery ruby-on-rails sunspot


    【解决方案1】:

    您可以在搜索表单中使用:remote =&gt; true 选项

    <%= form_tag index_by_dvp_path,:method => :get, :remote => true do %>
      <p>
       <%= text_field_tag :search, params[:search] %>
       <%= submit_tag "Search",remote: true %>
      </p>
    <% end %>
    

    并将表单提交给一个动作,该动作将在控制器中获取您的搜索结果。也用同样的方法回复format.js

    def index_by_dvp
      @values = Model.where(params[:search])
    
      respond_to do |format|
        format.js
      end
    end
    

    在 index_by_dvp.js.erb 中

    $('#your_div_for_search_result').html("<%= escape_javascript(render :partial => 'your_search_partial')%>")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2010-12-19
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多