Search code examples
ruby-on-rails

I´m having a similar problem. Somehow my route doesnt seem to be working when i try to delete one post on my blog.It does nothing instead of delete it


I can´t understand why it doesn´t delete one article when I push the button destroy on the show.html.erb file. If you can help...

i tried to see if it would even call the destroy function on the articles_controller.rb, and i think it doesn´t, don´t know why.

this is my routes.rb

Rails.application.routes.draw do
  root "articles#index"
  
  resources :articles
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

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

articles_controller.rb

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

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

  def new
    @article = Article.new(title: "...", body: "...")
  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
    
    redirect_to root_path, status: :see_other
  end


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

show.html.erb

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

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

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

Solution

  • I couldn´t figure out what i was doing wrong still if i change the «link_to» to «button_to» it works perfectly. So if are having this problem might be good to change that