Here's how to clean a normal string:
string = "<b>Bob</b>"
string = string.gsub(/<\/?[^>]*>/, "")
string = string.gsub(/<\/?[^>]*>/, "")
Also, another handy thing you can do is clean everything going into
your database. This code will automatically clean any string or text
data in your database. You don't even have to enter in the name of
the columns you want cleaned. All you have to do is change the name of the model(it's in orange below) and then enter in the code into your class model, ie: product.rb. And it will take care of the cleaning
for you:
class Product < ActiveRecord::Base
before_save :strip_html
def strip_html # Automatically strips any tags from any string to text typed column
for column in Product.content_columns
if column.type == :string || column.type == :text # if the column is text-typed
if !self[column.name].nil? # strip html from string if it's not empty
self[column.name] = self[column.name].gsub(/<\/?[^>]*>/, "")
end
end
end
end
before_save :strip_html
def strip_html # Automatically strips any tags from any string to text typed column
for column in Product.content_columns
if column.type == :string || column.type == :text # if the column is text-typed
if !self[column.name].nil? # strip html from string if it's not empty
self[column.name] = self[column.name].gsub(/<\/?[^>]*>/, "")
end
end
end
end
Keep in mind that this code will clean out ANYTHING that is in tags,
such as: <br><b><whatever><cheese><etc>


