Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Sinatra delete route with jQuery

I am pretty new to Sinatra and am making a simple todo app that utilizes basic CRUD functionality.

In the backend I have working routes and have tested everything. I wanted to incorporate some front end functionality and decided to use jQuery to help with that. I have a current piece of code in jQuery that adds a css class to one of the todo items when that item is clicked. I wanted to include a button that says "Delete completed tasks" that would collect the tasks with the class of "completed" and than trigger the Sinatra route which would delete the tasks from the database. The current Sinatra route is:

delete "/hub/note/:id" do
   n = Note.get params[:id]
   n.destroy
   redirect '/hub'
end

How do I get jQuery and Sinatra to communicate the delete the items with the class of "completed". Any help would be incredibly useful.

like image 654
Julian25 Avatar asked Dec 07 '25 07:12

Julian25


1 Answers

Utilizing the method_override trick/hack, you can get what you want by doing this:

jQuery.post("/hub/note/" + id, {_method: "delete"}, callback);

Anything leveraging Rack, including Sinatra and Ruby on Rails, should have this behavior.

like image 98
Max Avatar answered Dec 08 '25 22:12

Max