KNejad
KNejad

Reputation: 2508

Rails production delete link making get (show) request

When running Rails in development mode and I click a delete link like this:

<%= link_to "Delete", scenario_path(@scenario), method: :delete %>

It deletes the record as expected.

However when I run the same code in production it makes a request to the show page and doesn’t delete the record.

What could be causing this?

Upvotes: 0

Views: 103

Answers (1)

KNejad
KNejad

Reputation: 2508

Rails uses rails-ujs to submit a form to the server when the link is pressed. This is because links are always GET requests and rails requires a DELETE request to delete a record.

So if you have removed //= require rails-ujs from your application.js it won’t be loaded and requests will be GET instead of DELETE. Or if on an older version of Rails //= require jquery_ujs

This was what was causing the issue for me. So when I added rails-ujs it started working again. You can confirm this by disabling JS on your app and you will see that DELETE requests won’t work anymore. An alternative solution to adding rails-ujs is to use form buttons instead of links as described here: https://www.viget.com/articles/delete-in-rails-without-jquery-and-ujs/

<%= button_to "Delete", post_comment_path(@post, comment), method: :delete %>

Upvotes: 0

Related Questions