views:

13

answers:

2

Is there a way to filter out links posted in a comment or in a form. The datatype of the column where the field is stored might be text or varchar. I essentially want to strip off any kind of url embedded witin the content.

+1  A: 

You could try strip_links. From the Rails docs:

strip_links('<a href="http://www.rubyonrails.org"&gt;Ruby on Rails</a>')
# => Ruby on Rails

strip_links('Please e-mail me at <a href="mailto:[email protected]">[email protected]</a>.')
# => Please e-mail me at [email protected].

strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
# => Blog: Visit

There's also a strip_tags which removes any HTML tags from a string.

These functions won't be available in your model, however; only in your views. As far as I know, there's no built-in methods to accomplish this from the model. If you need to do it in the view, you could try extending the model with the relevant ActiveView class(es). Otherwise, it should be fairly easy to construct a regular expression to strip the links.

vonconrad
A: 

Aside form the already mentioned strip_links and strip_tags, there's a number of useful text helpers in ActionView.

Joost Schuur