最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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:

<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.

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

Thanks!

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:

<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.

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

Thanks!

Share Improve this question asked Feb 14, 2011 at 0:59 Don LeathamDon Leatham 2,7044 gold badges31 silver badges41 bronze badges 1
  • Check application/config.rb to see whether you're loading the JS files you think you are as defaults. (I was bit by this recently when I tried to make a rails app using jQuery instead of Prototype. The defaults were empty.) – Telemachus Commented Feb 14, 2011 at 1:40
Add a ment  | 

4 Answers 4

Reset to default 2

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.

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

I just ran into the same issue, newest Rails available up to date, and solved it by moving the :confirm option to html_options part of link_to arguments, like:

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

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

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:

// app/assets/javascript/application.js    
//= require jquery_ujs
发布评论

评论列表(0)

  1. 暂无评论