Can't destroy a article | can't change from the action controller to the destroy

78 Views Asked by At

I'm very new to Ruby on Rails and I m following this tutorial https://guides.rubyonrails.org/getting_started.html#using-partials-to-share-view-code Now I in the "7.5 Deleting an Article" I m facing an error. When I click on the "Destroy" nothing happens. I understand from my console that the destroy action is not occurring and the show is instead

When I click on the "destroy" this is the output:

Started GET "/articles/5" for ::1 at 2024-01-12 15:46:32 +0000
Processing by ArticlesController#show as HTML
  Parameters: {"id"=>"5"}
  Article Load (0.2ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
  ↳ app/controllers/articles_controller.rb:7:in "show"
  Rendering layout layouts/application.html.erb
  Rendering articles/show.html.erb within layouts/application
  Rendered articles/show.html.erb within layouts/application (Duration: 0.5ms | Allocations: 86)
  Rendered layout layouts/application.html.erb (Duration: 5.4ms | Allocations: 1223)
Completed 200 OK in 9ms (Views: 6.5ms | ActiveRecord: 0.2ms | Allocations: 1972)

show.html.erb

<h1><%= @article.title %></h1>

<p><%= @article.body %></p>

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

article_controller.rb

class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end

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

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render :new, status: :unprocessable_entity
    end
  end

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

  def update
    @article = Article.find(params[:id])
    if @article.update(article_params)
      redirect_to @article
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    Rails.logger.debug("Article destroyed successfully") # Adicione esta linha
    redirect_to root_path, status: :see_other
  end

  private
    def article_params
      params.require(:article).permit(:title, :body)
    end
end

route.rb

Rails.application.routes.draw do
  root "articles#index"
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
 
  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
  # Can be used by load balancers and uptime monitors to verify that the app is live.
  get "up" => "rails/health#show", as: :rails_health_check
  resources :articles

  # Defines the root path route ("/")
end

I really don't know what to do in this situation! I follow the tutorial and I don't understand how this error occurs to me.

1

There are 1 best solutions below

1
Pedro Diogo On

In the tutorial they don't put it but you need to set up turbo. You only need to add

<%= turbo_include_tags %>

in the

app/views/articles/show.html.erb

https://github.com/rails/rails/issues/44229