Francisco Núncio
Francisco Núncio

Reputation: 21

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>

Upvotes: 1

Views: 58

Answers (2)

BenFenner
BenFenner

Reputation: 1078

From your comment above, these seem to be your routes:

root_path         GET    /                             articles#index
articles_path     GET    /articles(.:format)           articles#index
                  POST   /articles(.:format)           articles#create
new_article_path  GET    /articles/new(.:format)       articles#new
edit_article_path GET    /articles/:id/edit(.:format)  articles#edit
article_path      GET    /articles/:id(.:format)       articles#show
                  PATCH  /articles/:id(.:format)       articles#update
                  PUT    /articles/:id(.:format)       articles#update
                  DELETE /articles/:id(.:format)       articles#destroy

Right now your "Destory" button links to article_path(@article) with no method so the default HTML verb is used, which is GET, which will navigate to your show action. You need to use the DELETE HTML verb instead. That can be accomplished by adding the method: :delete attribute to your link like so:

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

Don't be confused by the turbo-link stuff. That is extra fluff that doesn't get the real job done on its own. You might be better off avoiding it until you're more comfortable with the situation.

Upvotes: 0

Francisco N&#250;ncio
Francisco N&#250;ncio

Reputation: 21

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

Upvotes: 1

Related Questions