Skip to content

Instantly share code, notes, and snippets.

@joeljunstrom
Created November 9, 2010 08:22
Show Gist options
  • Save joeljunstrom/668856 to your computer and use it in GitHub Desktop.
Save joeljunstrom/668856 to your computer and use it in GitHub Desktop.
def create
@my_thing = MyThing.create(params[:my_thing])
respond_to do |format|
format.json { @my_thing.to_json }
end
end
$(document).ready(function() {
var $list = $('#list'),
$form = $('#my_form');
$form.bind('submit', function(event) {
event.preventDefault();
$.ajax({
url: $form.attr('action')+'.json',
type: 'POST',
dataType: 'json',
data: $form.serialize(),
success: function(json) {
var obj = json['my_model_name'];
$list.append('<li>'+obj['name']+'</li>');
$form.find(':input').each(function() {
switch(this.type) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment