Skip to content

Instantly share code, notes, and snippets.

@kellydunn
Created November 1, 2010 20:10
Show Gist options
  • Save kellydunn/658778 to your computer and use it in GitHub Desktop.
Save kellydunn/658778 to your computer and use it in GitHub Desktop.
ajax + otrack
// AJAX resource creation / deletion
// Currently follow's Rudy's conventions
function add_resource_by_listeners() {
$("#resource_submit").click(function(e){ // listen to the feature specific submit button
e.preventDefault();
var ajax_data = {
update_action : "create_resource", // "create_" + feature
resource : {
name : $("#resouce_name").val(),
link : $("#resournce_link").val()
}
};
project_update_ajax(ajax_data, function(){alert("yay");});
});
$("#remove_resource").click(function(){
var ajax_data = {
update_action : "delete_resource"
};
});
}
# app/models/project.rb
if params[:update_action] == "create_resource"
resource = Resource.new(params[:resource])
if resource.save
json = {:result => "success", :resource=>resource.attributes}
else
json = {:result => "failure", :errors=>"Could not save resource"}
end
elsif params[:update_action] == "delete_resource"
resource = Resource.find(params[:resource][:id])
if resource.destroy
json = {:result => "success", :resource=>resource.attributes}
else
json = {:resource => "failure", :errors=>"Could not delete resourc"}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment