简体   繁体   English

如何在Rails中将jQuery与Sunspot一起使用?

[英]How should I use jQuery with Sunspot in Rails?

I just want to display the search result in the same page without redirecting to default search link like ".....search=&commit=Search". 我只想在同一页面中显示搜索结果,而不重定向到默认搜索链接,例如“ ..... search =&commit = Search”。 My original intention is use jQuery to render the same form with the search result. 我的初衷是使用jQuery呈现与搜索结果相同的表单。 And the remote option is set below 远程选项设置如下

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

But when I click the search button, the page still redirects to the default link as I mentioned. 但是当我单击搜索按钮时,页面仍然重定向到我提到的默认链接。

I'm not sure where it the problem. 我不确定问题出在哪里。

You can use the :remote => true option for the search form 您可以在搜索表单中使用:remote => 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 %>

and submit the form to an action that will fetch the results of your search in controller. 并将表单提交给将在控制器中获取搜索结果的操作。 Also respond with format.js in the same method 还可以使用相同的方法使用format.js进行响应

def index_by_dvp
  @values = Model.where(params[:search])

  respond_to do |format|
    format.js
  end
end

in index_by_dvp.js.erb 在index_by_dvp.js.erb中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM