我应该如何在Rails中使用带有Sunspot的jQuery?(How should I use jQuery with Sunspot in Rails?)
我只想在同一页面中显示搜索结果,而不重定向到默认搜索链接,如“..... 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 %>但是,当我单击搜索按钮时,页面仍会重定向到我提到的默认链接。
我不确定问题出在哪里。
I just want to display the search result in the same page without redirecting to default search link like ".....search=&commit=Search". My original intention is use jQuery to render the same form with the search result. 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.
最满意答案
您可以对搜索表单使用: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 %>并将表单提交给将在控制器中获取搜索结果的操作。 也可以在同一方法中使用format.js进行响应
def index_by_dvp @values = Model.where(params[:search]) respond_to do |format| format.js end end在index_by_dvp.中
$('#your_div_for_search_result').html("<%= escape_javascript(render :partial => 'your_search_partial')%>")You can use the :remote => true option for the search form
<%= 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
def index_by_dvp @values = Model.where(params[:search]) respond_to do |format| format.js end endin index_by_dvp.
$('#your_div_for_search_result').html("<%= escape_javascript(render :partial => 'your_search_partial')%>")#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 9 条评论) |
本站网友 目前最好的浏览器 | 10分钟前 发表 |
search | |
本站网友 hrbar | 21分钟前 发表 |
remote | |
本站网友 双元 | 5分钟前 发表 |
本站网友 长沙万科金域华府 | 0秒前 发表 |
method => | |
本站网友 博爱堂 | 9分钟前 发表 |
true %> > </p> > <% end %> 但是 | |
本站网友 甜丫丫 | 27分钟前 发表 |
search] %> > <%= submit_tag "Search" | |
本站网友 罗渽民 | 29分钟前 发表 |
search | |
本站网友 雨过天晴免费版 | 14分钟前 发表 |
search] %> <%= submit_tag "Search" |