代码之家  ›  专栏  ›  技术社区  ›  Mohit Jain

Delete/Destroy在带有jQuery的Rails3中不起作用

  •  6
  • Mohit Jain  · 技术社区  · 14 年前

    我的删除/销毁不适用于Rails 3。

    不适用于任何脚手架,甚至不适用于新项目。

    <%= link_to 'Destroy', card, :confirm => 'Are you sure?', :method => :delete %> 
    

    this question . 解决方案是重新安装Firefox。但我的也不是在chrome,safari或opera工作。

    Html代码生成时间:--

     <a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
    

    PS:请不要说包含默认JS文件之类的。因为我对原型并不感兴趣,因为我使用的是jQuery。

    编辑/更新,重要: 当您根本不想使用原型时,这就是解决方案。我在我的项目中只使用jQuery和相应的插件。

    人们的回答是:首先包含prototype etc,然后安装gem etc来消除prototype和jQuery之间的冲突。那是垃圾。

    我已经贴出了答案。请在选择之前检查一下。为我工作了10多个项目,没有任何问题。您需要做的就是:

    4 回复  |  直到 7 年前
        1
  •  7
  •   Justin Houk    12 年前

    我遇到了Mohit遇到的相同问题,还需要在我的JavaScript资产中包含“不引人注目的JavaScript库”(或“ujs”)。在我当前的Rails(v3.2.5)中,将自动提供UJS库。您可以通过查看文件中的以下行来验证这一点:

    gem 'jquery-rails'

    在app/assets/javascripts中/应用程序.js文件:

    //= require jquery_ujs

    因为我不知道什么更好,我已经删除了 require jquery_ujs 我自己的线应用程序.js我花了一段时间才弄明白 link_to ..., :method => :delete 电话没用了!

    一旦我理解了这个问题,就可以很容易地将上面的两行添加回各自的文件中,然后一切又像预期的那样开始工作了。

        2
  •  9
  •   Mohit Jain    12 年前

    那你需要加上Jquery.rails.js在你的项目中,每当你试图删除任何东西,它会带你去显示页面。

    我不记得从哪里得到的解决方案和这个Jquery.rails.js文件。但肯定有可靠的消息来源。

    这是那个文件的代码。也许你会帮助别人。

    jQuery(function ($) {
        var csrf_token = $('meta[name=csrf-token]').attr('content'),
            csrf_param = $('meta[name=csrf-param]').attr('content');
    
        $.fn.extend({
            /**
             * Triggers a custom event on an element and returns the event result
             * this is used to get around not being able to ensure callbacks are placed
             * at the end of the chain.
             *
             * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
             *       own events and placing ourselves at the end of the chain.
             */
            triggerAndReturn: function (name, data) {
                var event = new $.Event(name);
                this.trigger(event, data);
    
                return event.result !== false;
            },
    
            /**
             * Handles execution of remote calls firing overridable events along the way
             */
            callRemote: function () {
                var el      = this,
                    data    = el.is('form') ? el.serializeArray() : [],
                    method  = el.attr('method') || el.attr('data-method') || 'GET',
                    url     = el.attr('action') || el.attr('href');
    
                if (url === undefined) {
                  throw "No URL specified for remote call (action or href must be present).";
                } else {
                    if (el.triggerAndReturn('ajax:before')) {
                        $.ajax({
                            url: url,
                            data: data,
                            dataType: 'script',
                            type: method.toUpperCase(),
                            beforeSend: function (xhr) {
                                el.trigger('ajax:loading', xhr);
                            },
                            success: function (data, status, xhr) {
                                el.trigger('ajax:success', [data, status, xhr]);
                            },
                            complete: function (xhr) {
                                el.trigger('ajax:complete', xhr);
                            },
                            error: function (xhr, status, error) {
                                el.trigger('ajax:failure', [xhr, status, error]);
                            }
                        });
                    }
    
                    el.trigger('ajax:after');
                }
            }
        });
    
        /**
         *  confirmation handler
         */
        $('a[data-confirm],input[data-confirm]').live('click', function () {
            var el = $(this);
            if (el.triggerAndReturn('confirm')) {
                if (!confirm(el.attr('data-confirm'))) {
                    return false;
                }
            }
        });
    
    
        /**
         * remote handlers
         */
        $('form[data-remote]').live('submit', function (e) {
            $(this).callRemote();
            e.preventDefault();
        });
        $('a[data-remote],input[data-remote]').live('click', function (e) {
            $(this).callRemote();
            e.preventDefault();
        });
    
        $('a[data-method]:not([data-remote])').live('click', function (e){
            var link = $(this),
                href = link.attr('href'),
                method = link.attr('data-method'),
                form = $('<form method="post" action="'+href+'">'),
                metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
    
            if (csrf_param != null && csrf_token != null) {
              metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
            }
    
            form.hide()
                .append(metadata_input)
                .appendTo('body');
    
            e.preventDefault();
            form.submit();
        });
    
        /**
         * disable-with handlers
         */
        var disable_with_input_selector = 'input[data-disable-with]';
        var disable_with_form_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')';
    
        $(disable_with_form_selector).live('ajax:before', function () {
            $(this).find(disable_with_input_selector).each(function () {
                var input = $(this);
                input.data('enable-with', input.val())
                     .attr('value', input.attr('data-disable-with'))
                     .attr('disabled', 'disabled');
            });
        });
    
        $(disable_with_form_selector).live('ajax:after', function () {
            $(this).find(disable_with_input_selector).each(function () {
                var input = $(this);
                input.removeAttr('disabled')
                     .val(input.data('enable-with'));
            });
        });
    });
    

    更新:

    你可以得到Jquery.rails.js从这里开始。

       https://raw.github.com/rails/jquery-ujs/master/src/rails.js
    
        3
  •  4
  •   Simon Perepelitsa    14 年前

    确保在布局中包含默认的Rails javascript文件。

    <%= javascript_include_tag :defaults %>
    
        4
  •  2
  •   Karan    12 年前

    确保在布局中包含默认的Rails javascript文件。

    <%= javascript_include_tag "application" %>