0

Trying to use a before_save in my Post model and then regex to replace anything that looks like an email with the word 'forbidden'. This is to cut down on spam from comments/posts created by users in a discussion board.

It is currently giving me a syntax error; but I am sure it is more than that? How do I fix it?

Post.rb

  before_save :remove_emails

  # Prevents and replaces any emails or URLs posted by user as <forbidden>
  def remove_emails
    self.post = post.gsub^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;%\$#\=~_\-]+))*$, "forbidden")
  end
James F
  • 916
  • 2
  • 11
  • 19

3 Answers3

2

According to doc,

  1. There is no gsub^ function. you have to use gsub or gsub!
  2. Pattern (first parameter) should be surrounded by '/' (slash)
barley
  • 4,403
  • 25
  • 28
1

remove the ^ right after gsub.

seph
  • 6,066
  • 3
  • 21
  • 19
  • still throwing syntax errors.. =[ I think it has to do with the actual formatting of the Ruby code, i suspect? – James F Feb 01 '12 at 15:46
1

With some adaptations on the Regexp posted in this question, you can try :

# Prevents and replaces any emails or URLs posted by user as <forbidden>
def remove_emails
  self.post.gsub!(/(http|https):\/\/[a-z0-9-\.]+([\-\.]{1}[a-z0-9-\.]+)*[a-z]{2,5}\S*/i, 'forbidden')
end
Community
  • 1
  • 1
Baldrick
  • 23,882
  • 6
  • 74
  • 79