使用jQuery调用Sinatra删除路由 [英] Call Sinatra delete route with jQuery

查看:95
本文介绍了使用jQuery调用Sinatra删除路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Sinatra的新手,我正在制作一个利用基本CRUD功能的简单todo应用程序。

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

在后端,我有工作路线,并测试了一切。我想结合一些前端功能,并决定使用jQuery来帮助解决这个问题。我在jQuery中有一段代码,当单击该项时,会向其中一个待办事项项添加一个css类。我想要包含一个删除已完成的任务按钮,该按钮将使用已完成类收集任务,而不是触发将从数据库中删除任务的Sinatra路径。目前的Sinatra路线是:

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

如何让jQuery和Sinatra将项目删除与已完成类进行通信。任何帮助都会非常有用。

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

推荐答案

利用 method_override 技巧/黑客,你可以通过这样做得到你想要的东西:

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

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

利用 Rack ,包括Sinatra和Ruby on Rails,应该有这种行为。

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

这篇关于使用jQuery调用Sinatra删除路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆