使用will_paginate Rails 4进行Ajax请求 [英] Ajax request with will_paginate Rails 4

查看:62
本文介绍了使用will_paginate Rails 4进行Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于SO的一些帖子,看看其他人是如何处理这个并且我正在使用的实现几乎存在但是在第三个链接请求后失败,会发生什么是重新加载页面而不是激活ajax请求。我也删除了Turbolinks,但这没有什么区别。

I've been reading through some posts on SO to see how others are handling this and the implementation i am using is almost there but fails after the third link requested, what happens is the page is reloaded as opposed to firing the ajax request. I have also removed Turbolinks but that hasn't made a difference.

这是我的设置,我认为js在某处失败,因为我的jQuery不是那么强大我希望有人可以提供帮助

This is my setup and I think the js is failing somewhere and as my jQuery is not that strong I was hoping someone could help out

Index.html.erb

<div class="facebook_ajax">
  <%= render 'shared/posts' %>
</div> 

shared / _posts.html.erb

<% @posts.each do |p| %>
<article class="post">
<%= image_tag('/assets/vandals_fb_profile.jpg') %>
<%= link_to 'Varsity Vandals', "https://www.facebook.com/VarsityVandals", target: 'blank' %>
<%= date_output(p.created_time) %>
  <figure>
    <% if p.large_image_url? %>
      <%= image_tag(p.large_image_url) %>
    <% end %>

    <% if p.video_url? %>
      <%= raw youtube_embed(p.video_url) %>
    <% end %>
  </figure>
  <p><%= p.message.gsub("\n", "<br>").html_safe %></p>
    <% if p.description? %>
      <p><%= p.description %></p>
    <% end %>
    <%= link_to 'Read More', p.link %>
</article>
<hr/>
<% end %>
<%= will_paginate @posts, previous_label: '',  next_label: '', page_links: false %>

index.js.erb

$('.facebook_ajax').html("<%= escape_javascript(render("shared/posts"))%>")

jQuery

<script>
      $(document).ready(function () {
        $(".facebook_ajax .pagination a").on("click", function(){
          $.getScript(this.href);
          return false;
        });
      });
    </script>

因此,当第一次点击下一个链接ajax请求完成时,但第三次点击时页面重新加载,网址更改为

So when clicking the next link for the first time ajax request completes fine, but upon the third click the page reloads and the url is changed to

?_=1399027258398&page=3

有人知道如何打击这个吗

Does anyone know how to combat this please

谢谢

推荐答案

以为我会回答这个问题,也许它可以帮助其他人,我不得不将事件委托给一直存在的DOM元素,所以使用了正文标签例如

Thought I would answer this so maybe it can help someone else, I had to delegate the event to a DOM element that was always there, so used the body tag for example

$(document).ready(function () {
        $("body").on("click", '.facebook_ajax .pagination a', function(e){
          e.preventDefault();
          $.getScript(this.href);
          return false;
        });
      });

我的理解是,一旦第一次点击,您就会失去收集的原始href集合

My understanding that is once the first click is made you lose the oriiginal set of href's that where gathered

良好链接这里

这篇关于使用will_paginate Rails 4进行Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆