所以我试图按照Ascii-cast:http://asciicasts.com/episodes/240-search-sort-paginate-with-ajax进行搜索(通过搜索表单和按钮)和排序(通过表格头部中的链接),而无需重新加载页面。我正在使用Firebug查看Firefox中的页面。
对于排序,我可以在Firebug中看到GET响应,当我在新选项卡中打开这些响应时,使用right:sort和:direction参数正确呈现partial,但不在当前选项卡中。这里发生了什么,它让我发疯了。
Ruby 1.8.7 Rails 3.0.9
find_friends.html.erb
<%= form_tag :controller => 'User', :action => 'find_friends', :method => 'get', :id => "usersearch" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :login => nil %>
</p>
<div id='findusers'><%= render :partial => 'findusers' %></div>
<% end %>
_findusers.html.erb
<!--Do some hidden field tags in order to get the sorting to persist after search -->
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<% puts "Before: " + params[:sort].to_s + ", " + params[:direction].to_s %>
<% if @users.any? %>
<table class= 'pretty' border='1px'>
<tr>
<th><%= sortable "login" %></th>
<th><%= sortable "email" %></th>
<th><%= sortable "created_at", "Date Joined" %></th>
<th> <u>Add </u></th>
</tr>
<% puts "After All: " + params[:sort].to_s + ", " + params[:direction].to_s %>
<% for u in @users %>
<tr>
<td> <%= u.login %> </td>
<td> <%= u.email %> </td>
<td> <%= u.created_at.strftime("%r %B %e, %Y") %> </td>
<td align="center">
<% unless @user.login == u.login %>
<%=link_to image_tag('add_friend.gif', :align=> 'center'),
:controller=>"Friendship",
:to_friend => u.id,
:to_friend_login => u.login,
:action => 'create',
:search => params[:search],
:direction => params[:direction],
:sort => params[:sort] %>
<% end %>
</td>
</tr>
<% end %>
</table>
<%= will_paginate @users %>
<% else %>
<p> Please provide a query in the search form to find users to add as a friend! </p>
<% end %>
的application.js
$(function() {
$("#findusers th a, #findusers .pagination a").live("click", function() {
$.getScript(this.href);
return false;
});
$("#usersearch input").keyup(function() {
$.get($("#usersearch").attr("action"), $("#usersearch").serialize(), null, "script");
return false;
});
});
find_friends.js.erb
<!--Div Tag findusers, Render Partial _findusers -->
$('#findusers').html("<%= escape_javascript(render(:partial => 'findusers')) %>");