0

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.

Amja
  • 1,315
  • 9
  • 25
  • Does the `Short` instance get created? – ian Feb 27 '13 at 19:27
  • @iain I'm not sure. How can I check that? – Amja Feb 27 '13 at 22:47
  • `create` will add the data to the database. Have a look in the database - which one are you using? – ian Feb 28 '13 at 09:43
  • Open the sqlite console by typing `sqlite3 path/to/database/file` and then `select * from shorts;` (or something similar) to get it to show you what's in the database. If you see the data, then it's definitely gone in. – ian Feb 28 '13 at 17:46
  • Its a ruby gem so I can't open the console – Amja Feb 28 '13 at 17:51
  • The only way the gem works is if you have Sqlite installed, which means you'll have a console. – ian Feb 28 '13 at 18:04
  • when I enter the `sqlite3` command, it says `-bash: sqlite3: command not found ` – Amja Feb 28 '13 at 18:08
  • That's a good indication of what might be the problem… make sure you've got Sqlite installed, and put up a list of the gems you've installed too. – ian Feb 28 '13 at 18:23
  • I know that I have SQLite and that it works because I've used it with other projects quite recently. – Amja Feb 28 '13 at 19:32
  • Ok, but you don't have a console and you have a problem related to calling a database library that doesn't throw an error in the calling code… If it was me, I wouldn't be as confident in saying "I know that it works" ;) – ian Feb 28 '13 at 20:30

0 Answers0