jjzjj

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

coder 2024-05-16 原文

在我的 index.html.erb 文件中,我试图显示我的对象的标题(“列表”)和正常的“显示”、“编辑”和“销毁”链接和/或按钮。使用 :method => :delete 和 :confirm => "are you sure?",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>

列表顶部的“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 不会。

如有任何关于调查内容以解决此问题的建议,我们将不胜感激。

谢谢!

最佳答案

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

关于javascript - Rails 3 - Javascript :confirm not working for link_to and button_to with :method => :delete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988021/

有关javascript - Rails 3 - Javascript :confirm not working for link_to and button_to with :method => :delete的更多相关文章

随机推荐