我试图用will_paginate gem实现一个Ajax调用,我发现这个指南http://ramblinglabs.com/blog/2011/11/rails-3-1-will_paginate-and-ajax看起来像一个简单的解决方案,虽然它包含了我不熟悉的coffeescript
我的代码如下
我的观点
<div class="container">
<div class="row">
<div id="userRecipes">
<%= render partial: 'userrecipes' %>
</div>
</div><!--/row-->
我的部分(userrecipes)
<% @recipes.each do |r| %>
<div class="span3">
<div class="thumbnail">
<%= image_tag r.avatar.url(:myrecipes) %>
</div>
<h4><%= link_to r.dish_name, r %></h4>
<hr>
<p><%= truncate r.description, :length => 90 %></p>
<p><%= link_to "Edit Recipe", edit_recipe_path(r.id) %></p>
<p><%= link_to "Delete Recipe", recipe_path(r.id), :confirm => "Are you sure?", :method => :delete %></p>
<p><%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create', :recipe_id => r.id}, {:method => :post } %></p>
</div><!--/span3-->
<% end %>
<%= will_paginate @recipes %>
userrecipes.js.erb文件
$('#userRecipes').html('<%= escape_javascript(render partial: 'userrecipes') %>');
$.setAjaxPagination();
的CoffeeScript
$ ->
$.setAjaxPagination = ->
$('.pagination a').click (event) ->
event.preventDefault()
loading = $ '<div id="loading" style="display: none;"><span><img src="/assets/loading.gif" alt="cargando..."/></span></div>'
$('.other_images').prepend loading
loading.fadeIn()
$.ajax type: 'GET', url: $(@).attr('href'), dataType: 'script', success: (-> loading.fadeOut -> loading.remove())
false
$.setAjaxPagination()
当我点击下一个锚标记以显示下一组结果时,页面保持原样并且没有新内容出现
当使用控制台查看是否有任何错误时我可以看到任何错误,输出是
GET http://localhost:3000/my_recipes?page=2&_=1355055997639
我在这里错过了什么吗?
同时在控制台中检查响应时,它还显示正在加载要加载的新配方,但视图中没有发生任何事情
赞赏任何指针
由于
答案 0 :(得分:1)
will_paginate创建的链接是html链接......
使用以下代码
$('.pagination a').attr('data-remote', 'true');
向他们添加数据远程true属性,以便他们向服务器发出js
请求...
答案 1 :(得分:0)
您应该在js.erb文件中添加它。
$('.pagination a').attr('data-remote', 'true');
这应该有效。