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 o开发者_StackOverflow社区f url embedded witin the content.
You could try strip_links
. From the Rails docs:
strip_links('<a href="http://www.rubyonrails.org">Ruby on Rails</a>')
# => Ruby on Rails
strip_links('Please e-mail me at <a href="mailto:me@email.com">me@email.com</a>.')
# => Please e-mail me at me@email.com.
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.
Aside form the already mentioned strip_links and strip_tags, there's a number of useful text helpers in ActionView.
精彩评论