Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a helper method to remove http or https from the beginning of a url in rails

I'm accepting user input in the form of web links, e.g.: http://google.com

In my database I don't want to store the http:// prefix, or https://. I was going to do a string search at the beginning of the URL for those two things.

I feel like this is something rails/ruby might do out of the box, does anyone know of such a function?

like image 693
Adam Avatar asked Oct 14 '25 09:10

Adam


1 Answers

From the document:

require 'uri'

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
uri.scheme   #=> "http"
uri.host     #=> "foo.com"
uri.path     #=> "/posts"
uri.query    #=> "id=30&limit=5"
uri.fragment #=> "time=1305298413"

Or,

require 'uri'

URI.split("http://www.ruby-lang.org/")
# => ["http", nil, "www.ruby-lang.org", nil, nil, "/", nil, nil, nil]
like image 98
sawa Avatar answered Oct 17 '25 02:10

sawa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!