Rails JavaScript数据库查询 [英] Rails javascript database queries
问题描述
如何通过javascript进行PUT,POST或DELETE调用?
How do I do a PUT, POST or DELETE call from javascript?
我已经使用jquery拖放收集了一些数据.然后如何将其发送到数据库?我想我也许可以使用jQuery.ajax,但无法确定其精确程度.
I've collected some data using jquery drag and drop. How do I then send this to the database? I think I might be able to use jQuery.ajax but can't work out how exactly.
任何指针都表示赞赏.
解决方案:
这是有效的javascript
This is the javascript that works
$(document).ready(function() {
$.ajax({
type: "PUT",
url: "/articles/8", // should be mapped in routes.rb
data: {articles:{name:"New Name"}}
});
});
但是它不会将第8条的名称更改为新名称". Firebug没有显示任何错误,但是我仍然无法将数据传递给编辑.
But it doesn't change the name of article 8 to New Name. Firebug shows no errors, but I still can't pass data to the edit.
该网址是使用put网址的标准更新,它存在于路由中.
The url is the standard update using put url, it exists in the routes.
推荐答案
您可以为此使用jQuery ajax:这是一种使用服务器端代码动态处理浏览器请求的好方法.
You can use jQuery ajax for this: It is a very good way to handle browser requests dynamically with server side code.
jQuery('#element_id').on('click change',function(){ // use event as per your need
$.ajax({
type: "GET",
url: "/edit_comment", // should be mapped in routes.rb
data: {comment:"new comment"},
datatype:"html", // check more option
success: function(data) {
// handle response data
},
async: true
});
});
有关更多详细信息,请检查以下链接:
For more details check these links:
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/category/ajax/
RAILs代码:
def edit_comment
@comment = params[:comment]
// store to database
response_to do |format|
render :html 'comment.html'
end
end
routes.rb
map.edit_comment "edit_comment", :controller => 'comment', :action => 'edit_comment'
这篇关于Rails JavaScript数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!