Rails: a method for "standardizing" URLs from users?

I want users in my Rails app to be able to share a link to their personal site in their profile. Simple enough, I have a column called website and a text box that they can fill in to make it work.

However, when I show this in a view, a problem arises. If I use the link_to , then regardless of whether the person will include http:// , whether the URL will work. But, if I automatically add http:// , then I will get it twice for users who DID put it in their url.

I am sure this is a fairly common problem, but I could not find any good blog posts or SO issues that affect it.

My question is: How do you handle the URL from users in your applications? What would you recommend as the most user-friendly way to standardize the URLs that are ultimately stored in db?

Thanks!

+4
source share
1 answer

If I understand your problem correctly, I would surely make sure that whenever a user saves his data, he checks and modifies (if necessary) the attribute of the website 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 
+6
source

All Articles