我下面他的Railscasts轨道AJAX教程和歌厅了一些麻烦。在现场搜索无法正常工作,我必须单击搜索按钮来获得结果。
I'm following the railscasts rails ajax tutorial and geting into some trouble. The live search does not work, I have to click the search button to get the result.
我有两个searchfilter。第一个是一个select_tag。第二个是一个复选框。
I have two searchfilter. First one is a select_tag. Second one is a checkbox.
下面是我的code:
results.html.erb
results.html.erb
<table> <tr> <td> <label for="name">Filter</label></br> <%= form_tag artist_search_path, :id => "artists_search" do %> <%= select_tag :titel_id, options_from_collection_for_select(titel.all, :id, :name, params[:titel_id]), include_blank: true, class: "form-control" %> <input type="checkbox" name="option[]" value="1" id="1"/><label></label></br> <%= button_to '#', class: 'btn btn-default' do %> <%= t(:find) %> <% end %> <% end %> </td> <td> <div id="searchresult"> <% @users.each do |user|%> <div> <div> <div> <div> <%= user.zip %> </div> </div> <div class="desc"> </div> <% end %> </td> </tr> </table>
的application.js
application.js
# Ajax search on keyup $('#artists_search input').keyup( -> $.get($("#artists_search").attr("action"), $("#artists_search").serialize(), null, 'script') false )
user.html.erb
user.html.erb
def result if params[:titel_id].present? @users = User.where('titel_id = ?', titel_id) if params[:option].present? @users = User.where(..) else @users = User.all end respond_to do |format| format.html # result.html.erb format.json { render :json => @users } end end
没有任何显示,JS控制台。
There is nothing showed in js console.
推荐答案Mime类型
这个问题可能是你发送一个 剧本申请,当你确实需要发送一个 JSON 要求:
The problem is likely that you're sending a script request, when you really need to be sending a json request:
$('#artists_search input').keyup( -> $.get($("#artists_search").attr("action"), $("#artists_search").serialize(), null, 'json') false )
这个问题是你使用的是 的respond_to 与 JSON (不使用 JS ),这意味着当你发送你的剧本的要求,也没有 MIME类型来提供响应
The issue is you're using the respond_to block with json only (no js), meaning that when you send your script request, there is no mime type to provide a response
除了在顶部的信息,该替代的方法是执行以下操作:
Apart from the information at the top, the alternative will be to do the following:
#app/controllers/users_controller.rb def search ... respond_to do |format| format.json format.js format.html end end
演示
我们已经这样做过这里:
我们用我们自己的JS捕捉 We used our own JS to capture the key-ups like this: 这是再处理如下:
更多推荐
Ruby on Rails的Live搜索(过滤)
发布评论