无法销毁文章 |无法从动作控制器更改为销毁

问题描述 投票:0回答:1

我对 Ruby on Rails 非常陌生,我正在学习本教程 https://guides.rubyonrails.org/getting_started.html#using-partials-to-share-view-code 现在我在“7.5 删除文章”中遇到错误。当我点击“销毁”时,没有任何反应。 我从控制台了解到,销毁操作没有发生,而是显示了

当我单击“销毁”时,这是输出:

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>

文章控制器.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

路线.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

遇到这种情况我真的不知道该怎么办!我按照教程操作,但我不明白这个错误是如何发生在我身上的。

ruby-on-rails model-view-controller controller action destroy
1个回答
0
投票

在教程中他们没有提到它,但你需要设置涡轮增压。 您只需添加

<%= turbo_include_tags %>

app/views/articles/show.html.erb

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

© www.soinside.com 2019 - 2024. All rights reserved.