Rails 3.1 link_to not showing confirmation or destroying properly

匆匆过客 提交于 2019-12-04 03:35:07

问题


I've been plowing through the chapters at railstutorial.org and been using Rails 3.1.3 because I'm crazy and/or wanted a challenge. I managed to figure out most version problems easily but this one stumped me for a while.

In 10.4.2, Michael Hartl uses the following code to delete users:

<%= link_to "delete", user, :method => :delete, :confirm => "You sure?", 
                            :title => "Delete #{user.name}" %>

It doesn't work properly if you test it in the browser (chrome) and instead sends you to that user page.

It is supposed to work if you include this:

<%= javascript_include_tag :defaults %>

but it fails with Rails 3.1 (it should work for Rails 3.0 though, or so I hear).


回答1:


So for all of you pulling out your hair for using Rails 3.1, here's the solution.

<%= javascript_include_tag "application" %>

Using "application" instead of :defaults solves this problem, delete and confirm should work, now get back to coding!

Special thanks to George Shaw for this answer over on https://stackoverflow.com/a/8350158/1127011 .

And it case you were wondering, title is for mouseover only.




回答2:


The a HTML tag will always fire the GET method. Rails uses the Javascript driver to replace the HTTP verb. The link_to method will fallback any method (post, put and delete) to get (corresponding to the show action) when :

  • Javascript has been disabled by the user
  • for some reason, Rails unobtrusive javascript driver is not handling the link properly

See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

I suspect the second reason to be the issue. Make sure the jquery.js, application.js, jquery_ujs.js file are included.

Instead of link_to, you could try to use button_to which creates a form allowing put, post and delete methods without Javascript enabled.



来源:https://stackoverflow.com/questions/8726534/rails-3-1-link-to-not-showing-confirmation-or-destroying-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!