1

i have some model, let it be Post with field :content. Any user can submit post with html (with links of course:) ) and i'd like to set nofollow on those links. Is there any rails plugin to automate this task? Does this plugin have ability to manage "nofollowing" in conditional way - e.g. admin can add links without nofollow, but other - with only nofollow?

z-index
  • 409
  • 8
  • 14
Alexey Poimtsev
  • 2,845
  • 3
  • 35
  • 63

2 Answers2

4

This should do what you're looking for: https://github.com/rgrove/sanitize/

Install the plugin, then for a block of text you can run:

<%=raw Sanitize.clean(@your_html, Sanitize::Config::BASIC) %>

There are other options that you can use to customise it, but the Config::BASIC version will detect all links in that block of text and add the nofollow tag to them.

-2

You can define a helper for this, overriding (or rather wrapping) the default link_to

In app/helpers/posts_helper.rb do something along the lines of:

def nf_link_to(link_text, post)
  opts = {}
  opts[:rel] = "nofollow" unless post.author == "admin"
  return link_to text, post, opts
end

So that in your view you can do:

<%= nf_link_to post.title, post %>

Which should result in:

<a href="/posts/12" rel="nofollow">My First[tm] Post</a>

You should have a good look at the actual implementation of link_to and make your ''nf_link_to'' as complex as (as in; passing arguments and perhaps a block) as you desire.

Hartog
  • 108
  • 7
  • -1: the problem is that link might be included in block of text submitted by the user, not how to generate such a link which is trivial. – mkk Jan 07 '14 at 21:34