4

I'd like Users in my Rails app to be able to share a link to their personal website in their profile. Simple enough, I have a column called website and a text field they can fill in to make it work.

However, when I display this in a view there's a problem. If I use the link_to helper then whether or not the person included the http:// determines whether or not the url will work. But, if I automatically prepend http:// then I get it twice for users who DID put it in their url.

I'm sure this is a fairly common problem, but I haven't been able to find any good blog posts or SO questions that address it.

My question is: How do you handle URL input from users in your apps? What would you recommend as the most user-friendly way to standardize the urls that end up being stored in the db?

Thanks!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Andrew
  • 42,517
  • 51
  • 181
  • 281

1 Answers1

7

If I understand your problem correctly I would probably make sure that whenever a user saves their data it checks and modifies (if needed) the website attribute using something like the following.

class User
  before_save :sanitize_website

  def sanitize_website
    unless self.website.include?("http://") || self.website.include?("https://")
      self.website = "http://" + self.website
    end
  end
end
Maran
  • 2,751
  • 15
  • 12