For example,
User adds this "iamsmelly.com".
And if I add an href to this, the link would be
www.mywebsite.com/iamsmelly.com
Is there a way to make it absolute if its not prepended by an http:// ?
Or should I revert to jQuery for this?
For example,
User adds this "iamsmelly.com".
And if I add an href to this, the link would be
www.mywebsite.com/iamsmelly.com
Is there a way to make it absolute if its not prepended by an http:// ?
Or should I revert to jQuery for this?
Probably a good place to handle this is in a before_save
on your model. I'm not aware of a predefined helper (though auto_link
comes somewhat close) but a relatively simple regexp should do the job:
class User < ActiveRecord::Base
before_save :check_links
def check_links
self.link = "http://" + self.link unless self.link.match /^(https?|ftp):\/\//
end
end
I've looked for something similar before with no luck. I made a helper method like so:
def ensure_absolute(str_link)
(str_link.include?("http://") || str_link.include?("https://")) ? str_link : ("http://"+str_link)
end