2

我正在使用 Rails 进行敏捷 Web 开发,我的 products_controller 中的 :destroy 方法只是将我带到产品展示页面。几周前,我在 Rails 3.0 应用程序上的另一个应用程序中遇到了这个问题。在那种情况下,我不得不编辑 javascript,所以我猜这也可能在这里工作。

这是我的 application.html.erb 文件中的 javascript_link_tag:

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tags %>

这是我的控制器中的 :destroy 方法:

def destroy
  @product = Product.find(params[:id])
  @product.destroy

respond_to do |format|
  format.html { redirect_to products_url }
  format.json { head :ok }

我已经安装了 jquery gem,并且 jquery 是 Rails 3.1 的默认库,所以也许我错了为什么这不起作用?任何帮助是极大的赞赏。谢谢!

4

5 回答 5

7

如果您不使用 javascript,则 :delete 操作只会将您带到该特定帖子。

如果要删除条目,请使用

<%= button_to "destroy", post, :method => :delete %>    

代替

<%= link_to "destroy", post, :method => :delete %>     

这是让 javascript 工作的方法(请确保 jquery 加载)。

在您的 jquery 代码之后从我的存储库中加载 rails.js。

在https://github.com/imjp/bluh查看我的存储库中的以下文件

app/assets/javascripts/rails.js
app/assets/javascripts/app.js
app/views/posts/index.html    look for the delete button at the bottom
app/views/controllers/posts_controller.rb   look for the destroy action

该代码现在应该使您的销毁链接通过 ajax 工作。

于 2011-06-19T08:26:28.437 回答
2

这是因为您没有包含正确的 js 文件,或者因为您没有提供 delete 方法。我看起来你做对了:

app/views/layouts/application.html.erb

<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

删除方法不起作用,因为 rails 3 需要来自 csrf 元标记的信息,该标记是由来自的 javascript 文件生成的:defaults

然后在您的link_to方法中确保包含以下内容:

<%= link_to "destroy", post, :method => :delete %>
于 2011-06-19T01:22:08.457 回答
1

我遇到了同样的问题...

检查我对此的回答Delete / Destroy is not working in rails 3 with Jquery

如果您使用的是 Jquery 而不是原型,那么您需要在您的项目中添加 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'));
        });
    });
});
于 2011-06-19T12:24:40.297 回答
1

您可能已经删除了 rails.js 文件,该文件包含读取由 link_to 方法创建的链接中的 data-method 属性的 javascript 代码,并更改了请求的动词(get、post、put、delete)。我遇到了完全相同的问题,因为我只是通过 javascript_include_tag 加载 jquery.js。

如果是这种情况,请确保您拥有 rails.js,它应该可以解决问题!

于 2011-11-13T21:05:44.800 回答
-2

敏捷 Web 开发建议将默认的 application.html.erb 更改为:

<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>

<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>

保持默认的rails,它应该可以正常工作。

于 2012-01-13T17:52:12.117 回答