Rails: method for 'standardizing' url input from users?
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开发者_Python百科 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!
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
精彩评论