3

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.

4

2 に答える 2

2

method_overrideトリック/ハックを利用して、これを行うことで必要なものを得ることができます:

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

Sinatra や Ruby on Rails など、Rackを利用するものはすべて、この動作を行う必要があります。

于 2012-04-22T18:35:54.127 に答える
0

あなたのjQueryコードで。

$.post('/user/' + user.id, {_method: 'delete'}, function(result_data) {
  console.log('DELETE result', result_data);
}).error(function(){
  console.log("Error trying to DELETE");
});

Sinatra コードで

delete "/user/:id" do
  if request.xhr?
    content_type :json
    id = params[:id].to_i
    user = User.find_by_id(id)
    if !user
      status 404
      return {:success => false, :message => "Invalid ID supplied."}.to_json
    end
    user.destroy
    return {:success => true, :id => id, :message => "The User with ID #{id} was deleted."}.to_json
  end
  status 403
  return "<html><head><title>Error 403</title></head><body><h2>Expected an AJAX call.</h2></body></html>"
end

シナトラ アプリがアプリでない場合は、他の sinatra アプリと一緒に行modularも含めてください。set :method_override, trueset

于 2012-11-28T03:43:45.817 に答える