将rel = nofollow添加到rails中的will_paginate链接

时间:2011-01-04 10:14:48

标签: ruby-on-rails will-paginate

有没有办法将“rel = nofollow”添加到由rails中的will_paginate gem创建的链接中?

4 个答案:

答案 0 :(得分:4)

我发现覆盖的page_number不起作用(并且也不会处理你的下一个和后一个链接)。相反,我重写rel_value:

require 'will_paginate/view_helpers/link_renderer'
class PaginationNoFollow < WillPaginate::ViewHelpers::LinkRenderer
  def rel_value(page)
    case page
    when @collection.previous_page; 'prev nofollow' + (page == 1 ? ' start nofollow' : '')
    when @collection.next_page; 'next nofollow'
    when 1; 'start nofollow'
    else
      'nofollow'
    end
  end
end

将此类添加到lib,并在您的视图中:

will_paginate @users, :renderer => PaginationNoFollow

答案 1 :(得分:1)

您需要创建自己的LinkRenderer并使用它

require 'will_paginate/view_helpers/link_renderer'
class PaginationNoFollow < WillPaginate::ViewHelpers::LinkRenderer

  def page_number(page)
    unless page == current_page
      link(page, page, :rel => 'nofollow')
    else
      link(page, page, :rel => 'nofollow', :class => 'on')
    end

  end

end

在您看来:

= will_paginate @users, :renderer => PaginationNoFollow

答案 2 :(得分:0)

在will_paginate 3和Rails 3中,我必须像这样重写ActionView :: LinkRenderer:

require 'will_paginate/view_helpers/action_view'

class PaginationNoFollow < WillPaginate::ActionView::LinkRenderer
  def rel_value(page)
    [super, 'nofollow'].compact.join(' ')
  end
end

答案 3 :(得分:0)

您还可以将以下元标记放入网页的<!-- Prevent webcrawlers to navigate paginations --> <% if params[:page] %> <meta name="robots" content="noindex"> <% end %> 部分:

$scope.calDate = function(date1, date2){
   var dateOut1 = new Date(date1); // it will work if date1 is in ISO format
   var dateOut2 = new Date(date2);
   var timeDiff = Math.abs(dateOut2.getTime() - dateOut1.getTime());
   var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
   return diffDays;
};

我发现这是一种更好的方法,因为你减少了对will_paginate gem的依赖。