找不到注释控制器 - Ruby 教程的操作'show'



我正在尝试按照以下指南启动Ruby on Rails。一切都很顺利,直到我试图破坏(删除评论)。

我收到以下错误:找不到注释控制器的操作"显示"

我将在下面发布我的代码。

http://guides.rubyonrails.org/getting_started.html

comments_controller.rb

class CommentsController < ApplicationController
  http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
    def create
        @article = Article.find(params[:article_id])
        @comment = @article.comments.create(comment_params)
        redirect_to article_path(@article)
    end
    def destroy
        @article = Article.find(params[:article_id])
        @comment = @article.comments.find(params[:id])
        @comment.destroy
        redirect_to article_path(@article)
    end
    private
        def comment_params
          params.require(:comment).permit(:commenter, :body)
        end
end

articles_controller

class ArticlesController < ApplicationController
 http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
    def index
       @articles = Article.all
    end
    def show
        @article = Article.find(params[:id])
    end
    def new
        @article = Article.new 
    end
    def edit
      @article = Article.find(params[:id])
    end
    def create
      @article = Article.new(article_params)
      if @article.save
        redirect_to @article
      else
        render 'new'
      end
    end
    def update
      @article = Article.find(params[:id])
      if @article.update(article_params)
        redirect_to @article
      else
        render 'edit'
      end
    end
    def destroy
      @article = Article.find(params[:id])
      @article.destroy
      redirect_to articles_path
    end
    private
      def article_params
        params.require(:article).permit(:title, :text)
      end
end

路线.rb

Rails.application.routes.draw do
  resources :articles do
    resources :comments
  end
  root 'welcome#index'
end

文章.rb

class Article < ActiveRecord::Base
    has_many :comments, dependent: :destroy
    validates :title, presence: true,
                      length: { minimum: 5}
end

评论.rb

class Comment < ActiveRecord::Base
  belongs_to :article
end

文章/索引.html.erb

<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>

只是不确定它会是什么。请指教。如果需要,我将提供更多代码。

答案将在您的routes.rb和您尝试调用的URL中。

URL可能是/comment/123的,并且routes.rb中可能包含类似get 'comment/:id' => 'comment#show'的内容(或用于注释的resource声明)。

因此,rails 尝试显示页面以供注释,而您没有实现该功能。错误消息准确地说出了它需要说的内容(您的CommentsController中确实没有.show方法)。

如果我的猜测不正确,请发布您的routes.rb和您请求的URL。

我知道

这已经很老了,但是对于那些希望开始使用Rails构建Web应用程序的人来说,教程"Getting Started with Rails"仍然是Google上的最佳结果。

我今天遇到了同样的问题,这是有道理的。如果要删除注释,不能简单地使用 GET 请求导航到它,需要使用 DELETE 方法。

我通过将link_to替换为button_to来解决这个问题,希望 Rails 能够理清"魔力",它确实做到了。

使用的代码:

<%= button_to 'Destroy Comment', [comment.article, comment],
                method: :delete,
                data: { confirm: "Are you sure?" } %>

而不是

<%= link_to 'Destroy Comment', [comment.article, comment],
                method: :delete,
                data: { confirm: "Are you sure?" } %>

你可以添加

def show
    @article = Article.find(params[:article_id])
    @comment = @article.comments.find(params[:id])
    @comment.destroy
    redirect_to article_path(@article)
end

到您的comments_controller.rb

实际上,这些代码与def destroy中的代码相同

我一直

在学习相同的教程,就在今天遇到了与原始海报完全相同的问题。

我没有确切地注意到它何时开始,但它似乎在第 6、7 或 8 节的某个地方开始(向博客添加评论、重构或添加评论删除)。

此外,单击应用程序内的链接(即"返回"链接)将生成一个新选项卡。 同样,单击"销毁"链接时,浏览器的确认对话框将显示在当前选项卡上(正确的行为),但同时会出现一个新的浏览器选项卡(不正确) - 我认为这种新的选项卡行为(一种"显示"用户正在决定是否继续销毁的评论的类型)是导致错误。

完成教程(再增加一节,基本身份验证)后,我退出并重新启动了Chrome,然后退出并重新启动了服务器(ctrl-c,然后是bin/rails服务器)。

这样做之后,一切都很好。生成额外选项卡行为以及触发的虚假显示操作(从而导致错误)——当销毁确认表出现时——都已停止。

因此,这个问题似乎只是一些虚假的噪音 - 可能是由最初编写教程和原始海报遇到这种行为(并且今天仍然存在)之间(在rails环境中?)引起的(在rails环境中?)仍然持续存在,使用rails 5.2)。

添加注释模型belongs_to :user如果您使用用户和其他您belongs_to的任何内容进行身份验证......

我遇到了一些类似的问题,例如:

  • 尝试删除评论时出现相同的消息:

找不到注释控制器的操作"显示"

  • 尝试删除文章时,它没有被删除,而是呈现其页面。

  • 删除文章和评论时,确认对话框未显示。

我尝试重新启动服务器以及上面的解决方案之一,即创建与destroy方法相同的show方法。好吧,它对于删除评论工作得很好,但考虑到 RoR DRY 模式,这对我来说似乎不对。

我最终意识到负责阻止JavaScript(NoScript 11.2.9)的浏览器扩展是活跃的,并导致了这些奇怪的行为。

在这种特殊情况下,它与轨道本身无关,而是与外部因素有关。

我有同样的问题,我试图将其添加到 comments_controller.rb 并且它可以工作

def show
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end

我遇到了同样的问题,但添加了

<%= javascript_pack_tag "应用程序"、"数据涡轮链接跟踪": "重新加载" %>

为我工作 Rails 6 和 Webpack 你应该使用它

更新:正如我之前提到的,您的CommentsController中没有show方法,因此您将无法访问任何commentshow页面。

正如您所说,您正在尝试访问此网址:localhost:3000/articles/2/comments/1这是comment具有id: 1 和 routes ( resources :comments ) 的显示页面,正试图将其带到show行动。 因此错误正确地抛出show could not be found for CommentsController.

请从浏览器中删除该网址,然后尝试再次访问article show页面。它会起作用。

相关内容