Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Rails应用程序中的javascript文件与数据库通信_Javascript_Jquery_Ruby On Rails - Fatal编程技术网

通过Rails应用程序中的javascript文件与数据库通信

通过Rails应用程序中的javascript文件与数据库通信,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,免责声明:我是rails新手,所以如果我在这个问题上完全错了,我道歉。我正在使用jQueryUI的可拖动功能,在用户拖动一个元素后,我需要向数据库中添加一个新条目。我的javascript如下所示: $(function() { $( ".staged" ).draggable({ appendTo: 'body', containment: 'window', scroll: false, helper: 'clone',

免责声明:我是rails新手,所以如果我在这个问题上完全错了,我道歉。我正在使用jQueryUI的可拖动功能,在用户拖动一个元素后,我需要向数据库中添加一个新条目。我的javascript如下所示:

$(function() {
    $( ".staged" ).draggable({ appendTo: 'body',
        containment: 'window',
        scroll: false,
        helper: 'clone',
        stop: function(e,ui) {
            // around here, I'd like to create a new entry in the database called "placed_images"
            var url = this.getAttribute("src");
            x = ui.offset.left;
            y = ui.offset.top;
            var div = document.createElement("div");
            div.className += "placed";
            div.innerHTML = '<img src="'+url+'" class="placed" style="top:'+y+'px;left:'+x+'px;">'
            document.getElementById('page').appendChild(div);
            $(function() {
                $( ".placed" ).draggable();
            });
        }
    });
});

除非没有表单,它应该异步进行,而不需要重新加载页面。就像我说的,这可能是一种愚蠢的方式,我不确定。但是,如果您知道一个库、gem或教程可能会对我有所帮助,那就是greaaat。

因为您使用的是jQuery,所以应该利用jQuery的内置AJAX方法从javascript与Rails服务器通信

jQuery有一个
$。ajax
方法:

例如:

$(function() {
    $( ".staged" ).draggable({ appendTo: 'body',
        containment: 'window',
        scroll: false,
        helper: 'clone',
        stop: function(e,ui) {
            // generate 'placed_images' on server
            $.ajax({
                data: { 'some_data_to_send_to_server':'any_data_goes_here' },
                type: 'POST',
                url: '/some_path_to_your_controller',
                success: function () {
                    // it worked!  
                },
                error: function (response) {
                    // we had an error
                }
            });

            var url = this.getAttribute("src");
            x = ui.offset.left;
            y = ui.offset.top;
            var div = document.createElement("div");
            div.className += "placed";
            div.innerHTML = '<img src="'+url+'" class="placed" style="top:'+y+'px;left:'+x+'px;">'
            document.getElementById('page').appendChild(div);
            $(function() {
                $( ".placed" ).draggable();
            });
        }
    });
});
$(函数(){
$(“.staged”).draggable({appendTo:'body',
遏制:“窗口”,
卷轴:错,
助手:“克隆”,
停止:功能(e、ui){
//在服务器上生成“放置的图像”
$.ajax({
数据:{'some_data_to_send_to_server':'any_data_go_here',
键入:“POST”,
url:“/some\u path\u to\u your\u controller”,
成功:函数(){
//成功了!
},
错误:函数(响应){
//我们犯了一个错误
}
});
var url=this.getAttribute(“src”);
x=ui.offset.left;
y=ui.offset.top;
var div=document.createElement(“div”);
div.className+=“放置”;
div.innerHTML=“”
document.getElementById('page').appendChild(div);
$(函数(){
$(“.placed”).draggable();
});
}
});
});

我刚刚回答了一个类似的问题,这应该有助于你成为救生员!工作得很有魅力。
$(function() {
    $( ".staged" ).draggable({ appendTo: 'body',
        containment: 'window',
        scroll: false,
        helper: 'clone',
        stop: function(e,ui) {
            // generate 'placed_images' on server
            $.ajax({
                data: { 'some_data_to_send_to_server':'any_data_goes_here' },
                type: 'POST',
                url: '/some_path_to_your_controller',
                success: function () {
                    // it worked!  
                },
                error: function (response) {
                    // we had an error
                }
            });

            var url = this.getAttribute("src");
            x = ui.offset.left;
            y = ui.offset.top;
            var div = document.createElement("div");
            div.className += "placed";
            div.innerHTML = '<img src="'+url+'" class="placed" style="top:'+y+'px;left:'+x+'px;">'
            document.getElementById('page').appendChild(div);
            $(function() {
                $( ".placed" ).draggable();
            });
        }
    });
});