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

发布于 2024-10-10 09:31:22 字数 63 浏览 3 评论 0原文

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

Is there any way to add "rel=nofollow" to the links created by the will_paginate gem in rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

以为你会在 2024-10-17 09:31:22

我发现覆盖 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

I find that overriding page_number does not work (and also wont take care of your next and back links). Instead, I override 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

Add this class to lib, and in your view:

will_paginate @users, :renderer => PaginationNoFollow
﹂绝世的画 2024-10-17 09:31:22

您需要创建自己的 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

You need create your own LinkRenderer and use it

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

In your view :

= will_paginate @users, :renderer => PaginationNoFollow
一腔孤↑勇 2024-10-17 09:31:22

在 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

In will_paginate 3 and Rails 3 I had to override ActionView::LinkRenderer like this:

require 'will_paginate/view_helpers/action_view'

class PaginationNoFollow < WillPaginate::ActionView::LinkRenderer
  def rel_value(page)
    [super, 'nofollow'].compact.join(' ')
  end
end
柠檬色的秋千 2024-10-17 09:31:22

您还可以将以下元标记放入页面的 部分:

<!-- Prevent webcrawlers to navigate paginations -->
<% if params[:page] %>
  <meta name="robots" content="noindex">
<% end %>

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

You can also place the following meta tag into the <head> section of your page:

<!-- Prevent webcrawlers to navigate paginations -->
<% if params[:page] %>
  <meta name="robots" content="noindex">
<% end %>

I found it to be a better approach as you reduce dependency on the will_paginate gem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文