ruby-on-railsruby-on-rails-7

Ruby On Rails 7 - Delete method not working


within my RoR project my delete method is not working. It's weird because it was working a day ago but now all it does it redirects me to the "friend" page. Another thing to note is that the pop up dialog of "are you sure?" also does not show up when deleting a friend when it was working previously. I read some solutions online stating to put "//= require jquery" and "//= require jquery_ujs" in your javascript file but all I could find was my manifest.js file in my "app/assets/config" directory.

Any help would be greatly appreciated.

index.html.erb

<% if user_signed_in? %>
  <table class="table table-striped table-bordered table-hover">
    <thead class="thead-dark">
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Twitter</th>
        <th>User ID</th>
        <th></th>
      </tr>
    </thead>

    <tbody>
      <% @friends.each do |friend| %>
        <% if friend.user == current_user %>
          <tr>
            <td>
            <%= link_to friend.first_name + " " + friend.last_name, friend, style: 'text-decoration:none' %>
            </td>
            <td><%= friend.email %></td>
            <td><%= friend.phone %></td>
            <td><%= friend.twitter %></td>
            <td><%= friend.user_id %></td>
            <td>
              <%= link_to 'delete', 
              friend,
              :method => :delete,
              :confirm => "are you sure?", 
              class: "btn btn-danger btn-sm" %>
            </td>
          </tr>
        <% end %>
      <% end %>
      
    </tbody>
  </table>

  <br>

<% else %>
  <h1>Welcome to the Friend App</h1>
<% end %>

manifest.js

//= link_tree ../images
//= link_tree ../builds
//= require jquery
//= require jquery_ujs

Solution

  • In Rails 7 the "old" way of specifying the delete method does not work. My guess is the change from rails-ujs to turbo is the culprit. Rails-ujs was moved into Rails as of version 5.1 and Hotwire Turbo replaces it in rails 7.

    This is how I solved my problem:

    route: destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy

    .html.erb:

    <%= link_to t('navigation.sign_out'), destroy_user_session_path, method: :delete, class: "btn btn-danger ml-3" %>
    

    html: (notice the data-method="delete")

    <a class="btn btn-danger ml-3" rel="nofollow" data-method="delete" href="/users/sign_out"><span class="translation_missing" title="translation missing: en.navigation.sign_out">Sign Out</span></a>
    

    Error: No route matches [GET] "/users/sign_out"

    Solved it with (solution source)

    .html.erb:

    <%= link_to t('navigation.sign_out'), destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
    

    .html: (notice the data-turbo-method="delete")

    <a data-turbo-method="delete" class="btn btn-danger ml-3" href="/users/sign_out"><span class="translation_missing" title="translation missing: en.navigation.sign_out">Sign Out</span></a>