最好的办法让will_paginate使用Ajax工作 [英] Best way to get will_paginate working with Ajax

查看:205
本文介绍了最好的办法让will_paginate使用Ajax工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你谷歌will_paginate和Ajax的,上面的结果是本博客帖子:但will_paginate的原作者说要不要使用此方法(坏的搜索引擎优化/蜘蛛)...

If you google "will_paginate" and "ajax", the top result is this blog post: But the original author of will_paginate says to not use this method (bad for SEO/spiders) ...

但我不能得到原作者的方法来工作(他的JavaScript杀死我所有的链接)。 的另一位先生提出了一个类似的方法mislav的(原will_paginate的作者)的概念。但我不能得到的的工作,无论是。

But I cant get the original authors method to work (his javascript kills all my links). An other gentleman suggests a similar method to mislav's (the original will_paginate author) concept. But I cant get that to work either.

所以....什么的是分页使用AJAX,并保持搜索引擎友好的最好方法是什么? (钢轨> 2.1)

so .... what is the best way to paginate using AJAX, and stay SEO friendly? (for RAILS >2.1)

推荐答案

Tomh的答案是正确的。只是为了shiggles,我原型的快速实现。 这里有一个截屏,显示它使用Ajax时启用Javascript(用户),并仍然有pretty的URL时JavaScript被禁用(谷歌)。这里有几个code片段,让您滚动就可以了。

Tomh's answer is correct. Just for shiggles, I prototyped a quick implementation. Here's a screencast that shows it using Ajax when Javascript is enabled (your users) and still having pretty URLs when Javascript is disabled (Google). And here are a few code snippets to get you rolling on it.

配置/ routes.rb中:

map.connect 'items/:page', :controller => "items", :action => "index", :page => 1

应用程序/控制器/ items_controller.rb:

class ItemsController < ApplicationController
  def index
    @items = Item.paginate(:all, :page => params[:page])

    respond_to do |format|
      format.html
      format.js do
        render :update do |page|
          page.replace_html :items, :partial => "items"
          page << "ajaxifyPagination();"
        end
      end
    end
  end
end

应用程序/视图/项目/ index.html.erb:

<h1>Listing items</h1>

<div id="items">
  <%= render :partial => "items" %>
</div>

应用程序/视图/项目/ _items.html.erb:

<%= will_paginate @items %>

<table>
  <% for item in @items %>
    <tr>
      <td><%= item.id %></td>
    </tr>
  <% end %>
</table>

布局:

<%= javascript_include_tag :defaults %>

公共/ Java脚本/ application.js中:

$(document).ready(function() {
    ajaxifyPagination();
});

function ajaxifyPagination() {
    $(".pagination a").click(function() {
    	$.ajax({
    	  type: "GET",
    	  url: $(this).attr("href"),
    	  dataType: "script"
    	});
    	return false;
    });
}

我的示例使用j​​Query的(与 jRails ),但它是简单的做原型为好。

My example uses jQuery (with jRails), but it's straightforward to do with Prototype as well.

这篇关于最好的办法让will_paginate使用Ajax工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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