1

New to rails. Was working on chapter 2 of railsTutorial.

I was pair programming with someone who only uses text based browser w3m. So once we created user resource, we created a new user and tried deleting it but couldn't. I could do it on my Chrome browser though, so thought this may be issue with JS or so.

How do we resolve this?

Aniruddha
  • 2,649
  • 4
  • 25
  • 40

2 Answers2

1

Rails relies on javascript for links to http DELETE actions.

link_to "Delete Thing", thing_path(@thing), method: :delete  

Generates a link with a data-method attribute. The Rails UJS driver then catches the click and turns it into into an AJAX HTTP DELETE request.

method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If href: '#' is used and the user has JavaScript disabled clicking the link will have no effect. If you are relying on the POST behavior, you should check for it in your controller's action by using the request object's methods for post?, delete?, patch?, or put?. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Added:

The whole reason we do this is that Rails is built around restful interfaces using the full range of HTTP methods; GET, POST, DELETE, PUT, PATCH. But some browsers can only send GET & POST requests and HTML does not define links which perform other actions than GET.

If no javascript is a requirement you can create a form to send requests other than GET:

<%= form_for @thing, method: :delete do |f| %>
   <%= f.submit 'Delete thing' %>
<% end %>

Note that this form uses method="post" for compatability. Rails adds a special hidden input to the resulting form:

<input name="_method" type="hidden" value="delete" />

Which Rails uses to fake the extended set of HTTP methods (DELETE, PUT, PATCH, etc).

max
  • 76,662
  • 13
  • 84
  • 137
0

If you don't want to use Turbolinks with your Rails(4) application, it's easy! Just do this:

  • Remove the gem 'turbolinks' line from your Gemfile
  • Remove the //=require turbolinks from your app/assets/javascripts/application.js
  • Remove the two "data-turbolinks-track" => true hash key/value pairs from your app/views/layouts/application.html.erb
G.B
  • 3,407
  • 2
  • 18
  • 47
  • Once I do this, would the destroy would go through? – Aniruddha Apr 30 '15 at 14:34
  • 1
    yup all the links will work fine. make sure to restart you server and browser – G.B Apr 30 '15 at 14:35
  • Downvoted. Turbolinks has nothing to do with your issue with handling delete requests. The Rails UJS driver (jquery-rails) is totally independent from Turbolinks, which simply speeds browsing by doing partial replacement. https://github.com/rails/turbolinks – max Apr 30 '15 at 14:46