0

I'm trying to learn Ruby on Rails and I'm getting stuck. When I try to delete a resource, nothing happens. It looks like it just refreshes the page. Looking around at other places on the Internet, I'm currently guessing that it has something to do with Javascript, but I'm not quiet sure what.

For reference, I'm following this guide and getting stuck on section 7.5: https://guides.rubyonrails.org/getting_started.html#deleting-an-article

I've been bumbling around the Internet for a couple hours now trying to figure this out to no avail. Here's a list of other questions I've looked at this I think might be related to this, but from which I haven't been able to find a solution: Ruby on rails destroy not working Destroy path not working in Ruby on Rails

So, here's my controller action in app/controllers/articles_controller.rb

class ArticlesController < ApplicationController
  def show
    @article = Article.find(params[:id])
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    flash[:success] = "The article has been destroyed."
    redirect_to articles_path, status: :see_other
  end
end

config/routes.rb is pretty simple

Rails.application.routes.draw do
  resources :articles
end

And here's how I'm trying to call that controller action from a view in app/views/articles/show.html.erb. I've seen a few different notations for how to call the delete method, none of them seem to work.

<ul>
  <li>
    <%= link_to "Destroy", article_path(@article), data: {
                  turbo_method: :delete,
                  turbo_confirm: "Are you sure?"
                } %>
  </li>
  <li>
    <%= link_to "Delete", article_path(@article), :method => :delete %>
  </li>
  <li>
    <%= link_to "Defenestrate", article_path(@article), method: :delete %>
  </li>
</ul>

And it's those links that just seem to do... nothing. As per some of things I found online, I tried including these lines (not at the same time) in app/views/layouts/application.html.erb

<%= javascript_include_tag :defaults, 'data-turbolinks-track' => true %>
<%= javascript_include_tag "defaults", 'data-turbolinks-track' => true %>
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>

But they all throw a similar error, something like:

Sprockets::Rails::Helper::AssetNotFound in Articles#show
Showing app/views/layouts/application.html.erb where line #10 raised:
The asset "defaults.js" is not present in the asset pipeline.

I'm assuming it's referring to some location either in app/assets or lib/assets but I'm not sure.

I tired adding gem "jquery-rails" to the Gemfile and including the following lines in app/assets/config/manifest.js

//= require jquery
//= require jquery_ujs

But... all to no avail. I seem to be completely stuck, so I'm hoping someone may be able to point me in the right direction. For reference, here are some versions

Rails 7.0.4
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
Fedora 37 (Server Edition)
  • 1
    `data: { turbo_method: :delete }` is the correct approach in Rails 7 with turbo. The rest of the stuff you have tried is old garbage from years ago. `//= require jquery` is a "magic comment" containing instructions for the old Sprockets assest pipeline. It doesn't do anything. Rails removed the jQuery dependence way back in 2017 and switched from `jquery_ujs` to `rails_ujs` which was replaced by Turbo in Rails 7. – max Nov 23 '22 at 13:06
  • 1
    Make sure you're using up to date sources as there were huge changes between how assets are managed in Rails 7 and the default js packages. https://guides.rubyonrails.org/working_with_javascript_in_rails.html#adding-npm-packages-with-importmap-rails – max Nov 23 '22 at 13:09
  • @max so you're saying I should remove those magic comments... done. But if ```data: { turbo_method: :delete }``` is the correct approach, why might it not be working? – ALittleHelpFromMyFriends Nov 26 '22 at 20:24
  • @max based on the link you sent, there is no file called ```importmap``` in my ```bin/``` directory. – ALittleHelpFromMyFriends Nov 26 '22 at 20:25
  • The easiest thing to do is the just use the `rails new foo` command to generate a working app and look at how its setup. There is a huge number of possible reasons why it isn't working and they depend on which version of rails you're using. It could be that the required assets aren't present on the page, a script error etc. If you just want to get on the with tutorial use `button_to` instead which doesn't rely on javascript. – max Nov 27 '22 at 06:14
  • This is probably the most common Rails noob question and the only real answer is to debug it until it works. – max Nov 27 '22 at 06:16

1 Answers1

1

Try with button_to in rails 7

<%= button_to "Destroy", @article, method: :delete %>
  • This... works, actually. But it stops working if I try to use the ```data: { turbo_method: :detele }``` – ALittleHelpFromMyFriends Nov 26 '22 at 20:26
  • 1
    You don't need or want to use `turbo_method` when using `button_to`. It creates an actual form which is posted and contains a hidden input named `_METHOD` which overrides the HTTP method header. You also have a typo - `detele` instead of `delete`. In this case you should use `method: :delete`. – max Nov 27 '22 at 06:19