简体   繁体   English

Ajax未加载到视图导轨3 will_paginate

[英]Ajax not being loaded to the view rails 3 will_paginate

Im trying to implement an Ajax call with the will_paginate gem, I found this guide http://ramblinglabs.com/blog/2011/11/rails-3-1-will_paginate-and-ajax which seemed like a simple solution, though it includes coffeescript which i am not familiar with 我试图用will_paginate gem实现Ajax调用,我发现此指南http://ramblinglabs.com/blog/2011/11/rails-3-1-will_paginate-and-ajax ,尽管它看起来很简单包括我不熟悉的咖啡脚本

My code is as follows 我的代码如下

My View 我的观点

<div class="container">
 <div class="row">
  <div id="userRecipes">
   <%= render partial: 'userrecipes' %>
 </div>
</div><!--/row-->

My partial (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 file 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()

When i click on the next anchor tag to show the next set of results the page stays as it is and no new content appears 当我单击下一个锚标记以显示下一组结果时,页面保持原样,并且没有新内容出现

When using the console to see if there are any errors i can see any, the output is 使用控制台查看是否可以看到任何错误时,输出为

GET http://localhost:3000/my_recipes?page=2&_=1355055997639

Am i missing something here? 我在这里想念什么吗?

Also Whilst inspecting the response in the console it is also showing that the new recipes to be loaded are being loaded but nothing is happening in the view 此外,在控制台中检查响应时,它还表明正在加载要加载的新配方,但视图中没有任何反应

Any pointers appreciated 任何指针赞赏

Thanks 谢谢

The links created by will_paginate are html links... will_paginate创建的链接是html链接...

use following code 使用以下代码

 $('.pagination a').attr('data-remote', 'true');

to add data-remote true property to them so they make an js request to the server... 向他们添加数据远程true属性,以便他们向服务器发出js请求...

You should add this in js.erb file. 您应该将此添加到js.erb文件中。

$('.pagination a').attr('data-remote', 'true');

this should work. 这应该工作。

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

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