I'm making a URL shortener application in Sinatra. It works as follows:
The front page is a form with one field, to enter a long url into:
<form action="" method="post">
<input type='url' name='url' placeholder='Enter URL to be shortened'>
<input type="submit">
</form>
The form posts to the same front page and the code for posting to '/' is this:
post '/' do
#Makes variable of POSTed url.
@long = params[:url]
loop do
#makes random six letter hash
@rand = (0...6).map{(65+rand(26)).chr}.join.downcase
#don't generate another one if it isn't found in the database
break if Short.first(id: "#{@rand}").nil?
end
#saves url and hash to database
@input = Short.create(url: @long, id: @rand)
#displays link with hash to be copied into browser address bar
"http://192.168.1.3:999/"+@rand
end
The problem is that when I submit the form, it doesn't return the http://192.168.1.3:999/...
or anything I put after the @input=Short.create(...
line. It doesn't return any errors, even when raise_on_save_failure
is true. If I comment that line out, it works fine (except when trying to use the shortened url).
EDIT: When I change the code to allow non-urls, it functions perfectly normally. It only breaks with the exact url format.