Ruby-on-rails 删除前 rails 确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19588058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:56:26  来源:igfitidea点击:

rails confirm before delete

ruby-on-railsruby-on-rails-3.2ruby-on-rails-4

提问by overflow

Here is my rails link_to

这是我的 rails link_to

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),data: {confirm: "Are you sure?"} %>

I tried the above method but it is directly deleting without any alert message. What wrong I made. Can any one correct it.

我尝试了上述方法,但它直接删除而没有任何警报消息。我犯了什么错误。任何人都可以纠正它。

回答by userxyz

Try this

尝试这个

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),method: :delete, data: {confirm: "Are you sure?"} %>

回答by Torsten

Answer for rails 4.1.8 (question doesn't include version)

rails 4.1.8 的答案(问题不包括版本)

and standard resource (also not specified)

和标准资源(也未指定)

  1. //= jquery_ujsmust be included in application.js (possibly missing)
  2. url is user_path(@user)
  3. specify :method => :delete(missing)
  4. confirm message within :data(was good)
  5. destroy methodin controller (not delete)
  1. //= jquery_ujs必须包含在 application.js 中(可能缺少)
  2. 网址是 user_path(@user)
  3. 指定:method => :delete(缺失)
  4. 确认消息:data(很好)
  5. 控制器中的销毁方法(不删除)

= link_to t(:delete) , user_path(@user), :method => :delete, :class => "btn btn-danger", :data => {:confirm => t(:are_you_sure )}

= link_to t(:delete), user_path(@user), :method => :delete, :class => "btn btn-danger", :data => {:confirm => t(:are_you_sure )}

The url in the question seems to create a GET, rather than a DELETE method. This may work if :method was specified. imho it is unfortunate that there is no seperate named url helper for this.

问题中的 url 似乎创建了一个 GET,而不是一个 DELETE 方法。如果指定了 :method,这可能会起作用。恕我直言,不幸的是,没有单独的命名 url 助手。

回答by LHH

link_to('Delete', {controller: :controller_name, id: id, action: :action_name}, confirm: "Are you sure you want to delete this?", method: :delete)

回答by sivamani

Rails confirmation popup box for destroy action:

用于销毁操作的 Rails 确认弹出框:

    <%= link_to 'Show', article_path(article),{:class=>'btn btn-success show_article' }%>
    <%= link_to 'Edit', edit_article_path(article),{:class=>'btn btn-warning edit'} %>
    <%= link_to 'Destroy', '#', "data-toggle"=>"modal", "data-target" => "#delete-#{article.id}",:class=>'btn btn-danger' %>
    <div class="modal fade" id="delete-<%= article.id %>" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">Confirmation Box</button>
          </div>
          <div class="modal-body">
            <p>Are you sure to delete this article</p>
          </div>
          <div class="modal-footer">
            <div class="modal-footer">
              <%= link_to 'Delete', article_path(article), method: :delete, :class => 'btn btn-danger' %>
                <a href="#" data-dismiss="modal" class="btn btn-warning">Cancel</a>
            </div>
          </div>
        </div>
      </div>
   </div>
</td>

回答by Marek Takac

  1. I think you should use :method option

    <%= link_to 'Delete',url_for(action: :delete,id: @user.id), method: :delete, confirm: "Are you sure?" %>

  2. It's probably better to use button (and form) for this kind of action

  1. 我认为你应该使用 :method 选项

    <%= link_to 'Delete',url_for(action: :delete,id: @user.id), method: :delete, confirm: "Are you sure?" %>

  2. 使用按钮(和表单)进行此类操作可能会更好

回答by Ganesh Kunwar

Check the following link:

检查以下链接:

<%= link_to 'Delete',url_for(action: :delete,id: @user.id), confirm: "Are you sure?" %>

回答by kristinalim

Make sure you have both the jQuery library and the jQuery driver for Rails included. The jquery-railsgem will take care of both of these in the later versions of Rails, from what I know.

确保包含 jQuery 库和 Rails 的 jQuery 驱动程序。据jquery-rails我所知,gem 将在 Rails 的更高版本中处理这两个问题。

Also, for better chances of compatibility with future versions of the jQuery driver, move the :confirmoption outside :dataand let the jquery-railsgem decide what to do with it.

此外,为了更好地与 jQuery 驱动程序的未来版本兼容,请将:confirm选项移到外面:data,让jquery-railsgem 决定如何处理它。