Rails 3 - Javascript:使用 :method => :delete 确认对 link_to 和 button_to 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4988021/
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-10-25 15:34:52  来源:igfitidea点击:

Rails 3 - Javascript :confirm not working for link_to and button_to with :method => :delete

javascriptruby-on-railslink-to

提问by Don Leatham

In my index.html.erb file I'm trying to display the title to my object (a "listing") and the normal "show", "edit", and "destroy" links and/or buttons. With :method => :delete and :confirm => "are you sure?", neither link_to or button_to will present the javascript confirmation box. Here is my index.html.erb:

在我的 index.html.erb 文件中,我试图显示我的对象的标题(“列表”)和正常的“显示”、“编辑”和“销毁”链接和/或按钮。使用 :method => :delete 和 :confirm => “你确定吗?”,link_to 或 button_to 都不会显示 javascript 确认框。这是我的 index.html.erb:

<h2><a href="#" onclick="alert('Hello world!'); return false;">Click Here for a Javascript test</a></h2>   
<table>  
  <tr>
    <th>List Title</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>    
<% @listings.each do |listing| %>
  <tr>
    <td><%= listing.title %></td>
    <td><%= button_to 'Show', listing %></td>
    <td><%= button_to 'Edit', edit_listing_path(listing) %></td>
    <td><%= button_to( 'Destroy', listing, 
                       :confirm => 'Are you sure?', :method => :delete)%> </td>
    <td><%= link_to "Destroy", listing, :confirm => "Are you sure?", :method => :delete %> </td>
  </tr>
<% end %>
</table>

The "Hello World" JS confirmation link at the top of the listing works perfectly, so I'm pretty sure that unobtrusive javascript is working fine for my app. (I also confirmed I have the necessary-- javascript_include_tag :defaults and csrf_meta_tag are in my application template.) The Rails documentation confirms the arguments that I'm using are supported (url, :confirm, :method). However neither button_to or link_to will generate the JS alert indicated by :confirm. One other bit of strangeness, with the exact same arguments, button_to will delete a record while link_to does not.

列表顶部的“Hello World”JS 确认链接工作正常,所以我很确定不显眼的 javascript 对我的应用程序运行良好。(我还确认我有必要的 - javascript_include_tag :defaults 和 csrf_meta_tag 在我的应用程序模板中。)Rails 文档确认支持我使用的参数(url、:confirm、:method)。但是 button_to 或 link_to 都不会生成 :confirm 指示的 JS 警报。还有一点奇怪,使用完全相同的参数, button_to 将删除一条记录,而 link_to 不会。

Any suggestions on what to investigate to fix this would be appreciated.

任何有关调查以解决此问题的建议将不胜感激。

Thanks!

谢谢!

采纳答案by Jordan T. Cox

I ran into this exact issue before when I was accidentally including both Prototype and jQuery. Check the resources that are getting loaded to ensure that you're not loading both frameworks somehow.

在我不小心同时包含 Prototype 和 jQuery 之前,我遇到了这个确切的问题。检查正在加载的资源,以确保您没有以某种方式加载这两个框架。

回答by Magesh

AFAIK this issue is caused due to the link_to option which causes problems when its method is anything other than GET (http method), so i use button_to option which works fine

AFAIK 这个问题是由于 link_to 选项引起的,当它的方法不是 GET(http 方法)时会导致问题,所以我使用 button_to 选项可以正常工作

回答by themarketka

I just ran into the same issue, newest Rails available up to date, and solved it by moving the :confirmoption to html_optionspart of link_toarguments, like:

我刚刚遇到了同样的问题,最新的 Rails 可用,并通过将:confirm选项移动到html_options部分link_to参数来解决它,例如:

<%= link_to (t '.scenarios.open'), { :controller => 'scenarios', :action => 'show', :name => scenario.name }, :confirm => 'really?' %>

Indeed, needed the jquery-railsgem, but that one was already here and properly linked.

确实,需要jquery-rails宝石,但那个宝石已经在这里并正确连接。

回答by Edu Lomeli

Check if Rails' Unobtrusive scripting adapter for jQuery(jquery_ujs) are loaded into your site, then you don't need to worry about third-party javascripts:

检查 Rails 的Unobtrusive jQuery 脚本适配器(jquery_ujs) 是否已加载到您的站点,然后您就不必担心第三方 javascripts:

// app/assets/javascript/application.js    
//= require jquery_ujs