I have a very basic app which hooks into the (http://is.gd) API, shortening any user-specified URL. Here's the current code I have to do this (works perfect as a static request):
# app.rb
require "sinatra"
require "open-uri"
def shorten_url(url)
open("http://is.gd/api.php?longurl=#{url}").read
rescue
nil
end
get "/" do
erb :index
end
get "/shorten?" do
if shorten_url(params[:url])
flash.now[:success] = "New URL: <strong>#{shorten_url(params[:url])}</strong>"
else
flash.now[:error] = "Woops, something went wrong when we tried to convert your URL. Please try again."
end
end
I am aware that this is a Sinatra app, but I'd be somewhat concerned if users with a Rails background couldn't understand it ;). Here's my index view file...
# index.erb / index.html.erb
<h4>Enter URL</h4>
<form action="/shorten" method="get">
<input type="text" name="url" placeholder="Enter URL to be shortened...">
<input type="submit" value="Shorten!">
</form>
So I've got all of that working, now the last thing I want to do is make it so that page doesn't refresh when you submit the form on the index page (AJAX). I know how to do this in jQuery, but is there by any chance a ruby gem that does this? If so, how is it used?