views:

392

answers:

2

I have these statements in a model:

before_save :add_http

protected
def add_http
  if (/^http\:\/\/.+$/.match(url)) == nil
    str = "http://" + url
    url = str
  end
end

I have checked the regex in the console and it appears to be right, but when the 'url' gets saved to the db the "http://" has not been added. Any ideas?

A: 

Nevermind, got it...

protected
def add_http
  if (/^http\:\/\/.+$/.match(url)) == nil
    str = "http://" + url
    self.url = str
  end
end
W_P
+1  A: 

Not sure if this matters to you or not, but your regex won't work with https URLs. This should work though:

def add_http
  self.url += "http://" if self.url.match(/^https?\:\/\/.+$/).nil?
end
Sarah Mei
Thanks, I thought about that but wanted to get the basics working first. By the way, I read your blog about the 'pron star' fiasco, nice article!
W_P