【问题标题】::destroy method not working in a Rails 3.1.0 rc2 application?:destroy 方法在 Rails 3.1.0 rc2 应用程序中不起作用?
【发布时间】:2011-06-19 00:49:02
【问题描述】:

我正在使用 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 的默认库,所以也许我错了为什么这不起作用?任何帮助是极大的赞赏。谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1


    【解决方案1】:

    如果您不使用 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 工作。

    【讨论】:

    • 太棒了,这个解决方案奏效了。我仍然不明白为什么 javascript 不起作用。这是一个 Rails 3.1 应用程序,我在 application.html.erb 中有一个指向默认 javascript 库 (jquery) 的链接。我已经安装了 jquery gem 并与应用程序捆绑在一起。有什么我想念的想法吗?
    • 以下是让 javascript 工作的方法(请确保 jquery 加载)。
    • 你能重新上传那个 repo 吗?
    【解决方案2】:

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

    app/views/layouts/application.html.erb

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      检查我对此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'));
              });
          });
      });
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 欢迎来到 StackOverflow!接受的答案看起来像是原始问题是由其他原因引起的 - 我编辑了这篇文章以反映这一点。
        【解决方案5】:

        Agile Web Development 建议将默认的 application.html.erb 更改为:

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

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

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

        【讨论】:

        • 你推荐的是 old 风格的这样做。自引入资产管道以来,您的代码中的第一个示例块是正确的。
        猜你喜欢
        • 2012-03-02
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        • 1970-01-01
        • 2013-08-11
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多