Skip to content

Instantly share code, notes, and snippets.

@amite
Forked from mr-rock/sinatra_jquery_test.rb
Created March 12, 2012 02:28
Show Gist options
  • Save amite/2019338 to your computer and use it in GitHub Desktop.
Save amite/2019338 to your computer and use it in GitHub Desktop.
An example of Sinatra working with Ajaxified JQuery based on some pieces of code published by Rafael George on the Sinatra Google Group.
require 'sinatra'
require 'dm-core'
require 'haml'
DataMapper.setup(:default, 'sqlite3::memory:')
class Message
include DataMapper::Resource
property :id, Serial
property :name, String
property :message, String
end
Message.auto_migrate!
get '/' do
haml :app
end
post '/new' do
@message = Message.new
@message.message = "#{params[:will]} will #{params[:you]} you"
@message.save
@message.message
end
__END__
@@ app
%html
%head
%title Sinatra JQuery Test
%script{:type => 'text/javascript', :src => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'}
:javascript
$(document).ready(function() {
$('#new_message').submit(function() {
$.post($(this).attr('action'), $(this).serialize(), function(data){
$('#message').append("<p>" + data + "</p>");
$('#new_message').each(function(){this.reset();});
}, "text");
return false;
});
});
%body
#footer
%h2 leave your message
#message
%form#new_message{:action => '/new'}
%p
%input.pink{:type => "text", :id => "will", :name => "will"}
%strong will
%input.pink{:type => "text", :id => "you", :name => "you"}
%strong you
%input{:type => "submit", :value => "post!"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment