What am I missing with this Ruby on Rails Destroy Action?

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)

Upvotes: 0

Views: 759

Answers (2)

user3443684
user3443684

Reputation: 1

In Gemfile

gem 'turbo-rails'

In application.js

import "@hotwired/turbo-rails"

Run Commands

$ bin/bundle install

$ bin/rails turbo:install

$ bin/rails server

Upvotes: 0

Hemangini Gohel
Hemangini Gohel

Reputation: 151

Try with button_to in rails 7

<%= button_to "Destroy", @article, method: :delete %>

Upvotes: 2

Related Questions