【问题标题】:The action 'show' could not be found for CommentsController - Ruby Tutorial找不到 CommentsController 的“显示”动作 - Ruby 教程
【发布时间】:2023-03-04 17:40:01
【问题描述】:

我正在尝试按照以下指南来启动 Ruby on Rails。一切都很顺利,直到我尝试销毁(删除 cmets)。

我收到以下错误: 找不到 CommentsController 的操作“显示”

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

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

cmets_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

routes.rb

Rails.application.routes.draw do

  resources :articles do
    resources :comments
  end

  root 'welcome#index'
end

article.rb

class Article < ActiveRecord::Base

    has_many :comments, dependent: :destroy

    validates :title, presence: true,
                      length: { minimum: 5}
end

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :article
end

articles/index.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>

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

【问题讨论】:

  • 你能不能把你的评论和文章模型过去
  • 刚刚发布了模型先生。谢谢!
  • 请发布出现错误的 URL。
  • localhost:3000/articles/2/cmets/1 是给我消息的链接。
  • 为什么我得到-1?无论如何..仍在​​努力!感谢任何帮助!

标签: ruby-on-rails ruby


【解决方案1】:

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

该 URL 可能是 /comment/123,并且 routes.rb 中可能包含类似 get 'comment/:id' =&gt; 'comment#show' 的内容(或用于 cmets 的 resource 语句)。

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

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

【讨论】:

  • 可能会刷新页面,我个人花了很多时间。 :)
  • 我正在使用资源
【解决方案2】:

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

我没有注意到它到底是什么时候开始的,但它似乎是从第 6、7 或 8 节的某个地方开始的(将 cmets 添加到博客、重构或添加删除 cmets)。

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

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

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

所以看来这个问题只是一些虚假的噪音 - 可能是由于在最初编写教程和原始海报遇到这种行为(并且仍然存在)之间发生了一些变化(在 rails 环境中?)今天,18 年 7 月,使用 rails 5.2)。

【讨论】:

    【解决方案3】:

    我知道这已经过时了,但是对于那些希望开始使用 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?" } %>
    

    【讨论】:

      【解决方案4】:

      你可以添加

      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中的代码相同

      【讨论】:

        【解决方案5】:

        添加评论模型belongs_to :user 如果您与用户和其他人进行身份验证,无论您属于什么...

        【讨论】:

          【解决方案6】:

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

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

          无法为 CommentsController 找到“显示”操作

          • 当试图删除一篇文章时,它并没有被删除,而是呈现它的页面。

          • 既不删除文章也不删除评论时,没有显示确认对话框。

          我已尝试重新启动服务器以及上述解决方案之一,即创建与destroy 方法相同的show 方法。好吧,它可以很好地删除 cmets,但考虑到 RoR DRY 模式,这对我来说似乎不合适。

          我最终意识到负责阻止 JavaScript (NoScript 11.2.9) 的浏览器扩展程序处于活动状态并导致了这些奇怪的行为。

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

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              【解决方案8】:

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

              为我工作 rails 6 和 webpack 你应该使用它

              【讨论】:

                【解决方案9】:

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

                正如您所说,您正在尝试访问此 url:localhost:3000/articles/2/comments/1,这是具有 id: 1 的 comment 的显示页面,并且路由 (resources :comments) 正试图将其带到 show 操作。因此正确地抛出了错误show could not be found for CommentsController。

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

                【讨论】:

                • 所以我只是在里面放了一个空的节目?示例:def show end?
                • 不,将 show 放入您的控制器将无法正常工作,因为它会在错误的控制器中寻找 show 方法。
                • 可能是依赖问题,请确保您的文章模型具有正确的关联依赖has_many :comments, dependent: :destroy
                • @Cyzanfar 但他不是在破坏Article 而是Comment 对吗?...我没有清楚地理解您的意思,请您解释一下。
                猜你喜欢
                • 2012-05-12
                • 2015-02-23
                • 2013-07-03
                • 1970-01-01
                • 2018-08-08
                • 1970-01-01
                • 2013-01-19
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多