Setting up a Rails model to check for “http(s)” at the front of a URL

This issue arose when I asked my brother to test drive the Rails app I’m working on, UpLearn, without any supervision. It was really useful to have a second person use the software without any knowledge of how it was built, as issues surfaced that I might have otherwise missed.

One issue was the handling of URLs submitted by the user without an “http(s)” at the front. My brother had typed a link in to the submission form directly, rather than copy-pasting the URL, so it was missing an “http://” or “https://” at the front. As a result, my Rails app treated this as a relative path, rather than an absolute path, and the result was a broken link that didn’t take the user to the correct resource page.

For example, submitting this link (without the http or https part)

rather than

resulted in my Rails app creating a link on the resource details page that looked like this:

This obviously doesn’t take the user to the intended resource page and is not the desired outcome.

So, I needed a fix that would check for the presence of an http or https at the start of a link, and then prepend one to the link if there wasn’t one. I knew that using Regex might be one way to do it, but I started by googling the problem to see what else might work.

The question has been asked here on Stack Overflow and it’s a good starting point, exploring several different solutions.

There is a Ruby module called URI which sounds promising. It provides classes to handle Uniform Resource Identifiers (of which, Uniform Resource Locators, or URLs, are a subset).

So I started by jumping into my terminal window, opening Pry (a Ruby replicator, an alternative to IRB), and working with URI to see what it can do, as follows:
Begin by opening Pry (or equivalent):

Then grab the URI library:

Next, type in an example link, without an http at the front:

Then we can use the URI library as follows:

So, what’s going on here? Well, on line 1, I call the URI method, which returns the link converted to a URI object (meaning we can use the URI object methods on it). I assign this to the variable u.

On line 3, I call the URI method “scheme” on variable u. This parses u and looks for a scheme (“http” or “https” in this case). However, as the original link was just “”, there is no scheme in this instance, so the code just returns “nil” on line 4 .

On line 5, I simply prepend “http://” to the front of the link. Then I call the URI method on link again and reassign it to variable u, in line 7.

Now when I run u.scheme on line 9, the method returns “http” as the link now has a scheme.

This is the basic scenario that I’ll implement in the links model of my Rails app.

Implementing URI library in a Rails app

I added the following code to the link.rb model of my app, with the yellow shading showing the new lines:

It’s very similar to the code I typed into Pry when I was working the solution out. I’ve put the logic into a protected method called url_check, which I call before saving the link to the database using a before_save hook.

In the method itself, I use the URI method to convert the link to a URI object. Then I call .scheme method to find the scheme if one exists. If no scheme is found, i.e. the method returns “nil”, then we enter into the if statement (because of the “!”). Here I simply prepend “http://” to the link, using the “self” reference to identify the link in question.

Now it doesn’t matter whether a user submits “” or “” because the link model will handle either case before the link is saved, thereby ensuring the link has at least an “http” at the front.

This will now avoid the issue of my Rails app treating links without an “http” as relative links.

Leave a Reply

Your email address will not be published. Required fields are marked *