Rails 3.1 link_to does not show confirmation or destruction properly

I plowed in the chapters on railstutorial.org and used Rails 3.1.3 because I was crazy and / or wanted a challenge. I was able to easily find most version issues, but this question puzzled me a bit.

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

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

This does not work properly if you test it in a browser (chrome) and send the user to this page instead.

It should work if you enabled this:

 <%= javascript_include_tag :defaults %> 

but it does not work with Rails 3.1 (it should work for Rails 3.0, though, as I heard).

+4
source share
2 answers

So, for all of you, pulling out your hair to use Rails 3.1, here is the solution.

 <%= javascript_include_tag "application" %> 

Using "application" instead of: defaults solves this problem, deletes and confirms that the work should work, now back to the coding!

Special thanks to George Shaw for this response to fooobar.com/questions/873780 / ....

And this is a case that you were interested in, the heading is for the mouse only.

+3
source

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

  • Javascript disabled by user
  • for some reason, the Rails unobtrusive javascript driver does not properly handle the link

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

I suspect this is the second reason. Make sure jquery.js, application.js, jquery_ujs.js files are included.

Instead of link_to, you can try using button_to, which creates a form that allows you to use the put, post, and delete methods without Javascript enabled.

0
source

Source: https://habr.com/ru/post/956049/


All Articles