Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将rel=nofollow添加到将分页rails中的链接_Ruby On Rails_Will Paginate - Fatal编程技术网

Ruby on rails 将rel=nofollow添加到将分页rails中的链接

Ruby on rails 将rel=nofollow添加到将分页rails中的链接,ruby-on-rails,will-paginate,Ruby On Rails,Will Paginate,有没有办法将“rel=nofollow”添加到will_paginate gem in rails创建的链接中?您需要创建自己的链接渲染器并使用它 require 'will_paginate/view_helpers/link_renderer' class PaginationNoFollow < WillPaginate::ViewHelpers::LinkRenderer def page_number(page) unless page == current_pag

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

您需要创建自己的链接渲染器并使用它

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

我发现覆盖页码不起作用(也不会处理你的下一个和上一个链接)。相反,我覆盖了rel_值:

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

在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
require'will_paginate/view_helpers/action_view'
类PaginationNoFollow
您还可以将以下元标记放入页面的
部分:

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

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

require 'will_paginate/view_helpers/action_view'

class PaginationNoFollow < WillPaginate::ActionView::LinkRenderer
  def rel_value(page)
    [super, 'nofollow'].compact.join(' ')
  end
end
<!-- Prevent webcrawlers to navigate paginations -->
<% if params[:page] %>
  <meta name="robots" content="noindex">
<% end %>