有什么方法可以将“rel=nofollow”添加到由 rails 中的 will_paginate gem 创建的链接中?
问问题
1173 次
4 回答
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
于 2011-04-13T18:55:17.530 回答
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
于 2011-01-04T10:27:24.837 回答
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
于 2012-08-22T14:44:22.250 回答
0
您还可以将以下元标记放入<head>
页面的部分:
<!-- Prevent webcrawlers to navigate paginations -->
<% if params[:page] %>
<meta name="robots" content="noindex">
<% end %>
我发现它是一种更好的方法,因为您减少了对 will_paginate gem 的依赖。
于 2017-12-03T18:30:46.213 回答