How to add nofollow to links in Rails

It’s really very simple, but this one took me a while to figure out, so I thought I’d post it in case it helps someone else.

If you want to add a rel='nofollow' attribute to a link generated with a Rails helper, you just need to specify it in the html_options hash argument to link_to (or whatever link helper you’re using).

For example:

<%= link_to 'Vote', { :action => 'vote' }, { :rel => 'nofollow' } %>

Voila! This should discourage web crawlers (such as the Google search crawler) from following links they shouldn’t and generating false ‘clicks’.

3 thoughts on “How to add nofollow to links in Rails

  1. Thanks! Suggestion – don’t copy and paste this code. WordPress puts fancy quotes in place of the normal quotes, which will give you a syntax error!

  2. Thank you for pointing that out, Dave! I’ve updated the code snippet and copy and pasting should now work okay. (I’m using an explicit HTML code to prevent WordPress converting it.)

Leave a Reply

Your email address will not be published. Required fields are marked *