Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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与jQueryAjax_Jquery_Ruby On Rails - Fatal编程技术网

Rails与jQueryAjax

Rails与jQueryAjax,jquery,ruby-on-rails,Jquery,Ruby On Rails,我大部分时间都在工作。我的rails链接是: <%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :confirm => 'Are you sure?', :id => 'trash') %> 它隐藏了我点击的垃圾图标所在的行。这也很有效。我以为我已经解决了所有的问题,然后我遇到了这个问题。当你点击我的垃圾桶时,我会弹出这个确认框

我大部分时间都在工作。我的rails链接是:

<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :confirm => 'Are you sure?', :id => 'trash') %>
它隐藏了我点击的垃圾图标所在的行。这也很有效。我以为我已经解决了所有的问题,然后我遇到了这个问题。当你点击我的垃圾桶时,我会弹出这个确认框问你是否确定。无论您选择取消还是接受,jquery都会激发并隐藏行。它不会被删除,只会在刷新页面之前隐藏。我尝试更改它,以便通过jquery完成提示,但是不管我在提示中选择什么,rails都会删除该行,因为在调用提示时调用了.destroy函数

我的问题是如何从rails确认弹出窗口中获取要取消或接受的值,以便在我的jquery中有一个if语句,该语句在它们单击accept时隐藏,而在单击cancel时不执行任何操作

编辑:回答下面的问题。 那是行不通的。我尝试将链接更改为:

<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => "delete",  :onclick => "trash") %>
但函数从未被调用。它只是在没有提示的情况下删除它,并没有隐藏它。 但这给了我一个想法

我使用ajax调用的delete函数

     $('a.delete').click (function(){
            $.post(this.href, {_method:'delete'}, null, "script");
            $(row).hide();
});
并通过实现您的代码对其进行了修改:

删除
:确认=>“确定吗?”

$('a.delete').click (function(){
        if(confirm("Are you sure?")){
            var row = $(this).closest("tr").get(0);
            $.post(this.href, {_method:'delete'}, null, "script");
            $(row).hide();
            return false;
        } else {
            //they clicked no.
            return false;
        }

});
这会起作用。

您可以删除:

:confirm => 'Are you sure?'
并将其替换为自定义onclick事件,如下所示:

<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :onclick => 'someJSFunction', :id => 'trash') %>

很抱歉,我现在没有时间测试它,但是应该可以工作。

删除:确认=>“你确定吗?”

$('a.delete').click (function(){
            if(confirm("Are you sure?")){
                var row = $(this).closest("tr").get(0);
                $.post(this.href, {_method:'delete'}, null, "script");
                $(row).hide();
                return false;
            } else {
                //they clicked no.
                return false;
            }

    });

感谢各位,我使用以下教程使用rails设置jquery:

与本教程结合使用时,我使用了以下代码:

Query.fn.deleteWithAjax = function() { this.removeAttr('onclick'); this.unbind('click', false); this.click(function() { if(confirm("Are you sure?")){ $.delete_($(this).attr("href"), $(this).serialize(), null, "script"); return false; } else { //they clicked no. return false; } }) return this; }; Query.fn.deleteWithAjax=函数(){ 这个.removeAttr('onclick'); 此参数为.unbind('click',false); 单击(函数(){ 如果(确认(“你确定吗?”){ $.delete_($(this.attr)(“href”),$(this.serialize(),null,“script”); 返回false; }否则{ //他们单击“否”。 返回false; } }) 归还这个; };
我有一个类似的问题,但在阅读此线程后,我从按钮中删除了确认,并在jQuery中执行了以下操作:

$('.delete_post').on('click', function(){
    if(confirm("Are you sure?")){
         $(this).closest('tr').delay().fadeOut();
     } else{
        return false;
    }
});`
它似乎对我起了作用。我还在控制器中的销毁操作中添加了以下内容:

 respond_to do |format|
   format.html { render :nothing => true }
   format.json { head :no_content }
 end

注意:别忘了将
:remote=>true
添加到
链接_

您的意思是:remote=>true吗? Query.fn.deleteWithAjax = function() { this.removeAttr('onclick'); this.unbind('click', false); this.click(function() { if(confirm("Are you sure?")){ $.delete_($(this).attr("href"), $(this).serialize(), null, "script"); return false; } else { //they clicked no. return false; } }) return this; };
$('.delete_post').on('click', function(){
    if(confirm("Are you sure?")){
         $(this).closest('tr').delay().fadeOut();
     } else{
        return false;
    }
});`
 respond_to do |format|
   format.html { render :nothing => true }
   format.json { head :no_content }
 end