如何修复生成的 ERB 中出现的额外数据 HTML

How to fix extra data appearing in the ERB generated HTML

我正在按照 ruby-on-rails 说明指南创建一个简单的 blog 网络应用程序:https://guides.rubyonrails.org/getting_started.html#generating-a-controller

我的所有项目文件都与指南中的几乎相同。

app/views/articles/show.html.erb

<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>

<h2>Add a comment:</h2>
<%= render 'comments/form' %>

<h2>Comments (<%= @article.comments.count %>)</h2>
<%= render 'comment_section' %>
<%#= render @article.comments %>

<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Delete', article_path(@article),
            method: :delete,
            data: {confirm: 'Are you sure?'} %> |
<%= link_to 'Back', articles_path %>

app/views/comments/_form.html.erb

<%= form_with(model: [@article, @article.comments.build], local: true) do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>

app/views/articles/_comment_section.html.erb

<% if @article.comments.count > 0 %>
  <%= render @article.comments %>
<% else %>
  <p>There are no comments yet!</p>
<% end %>

app/views/comments/_comment.html.erb

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>

<p>
  <%= link_to 'Delete comment', [comment.article, comment],
              method: :delete,
              data: {confirm: 'Are you sure you want to delete this comment?'}
  %>

一篇没有评论的简单文章按预期工作:

但是,当显示带有一些实际评论的文章时,最后会显示一个额外的空评论:

当我尝试删除该评论时出现以下错误(路径中的 11 是 article_id):

删除其他评论没问题。

我认为可能相关的其他文件:

app/config/routes.rb

Rails.application.routes.draw do
  get 'welcome/index'

  resources :articles do
    resources :comments
  end

  root 'welcome#index'
end

app/models/article.rb

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

app/models/comment.rb

class Comment < ApplicationRecord
  belongs_to :article
end

app/controllers/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
  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

app/controllers/comments_controller.rb

class CommentsController < ApplicationController
  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

我正在使用:

ruby 2.6.5p114

Rails 6.0.0

sqlite3 3.8.7.2

RubyMine 2019.2.3

我正在开发 Windows

在你的views/comments/_comment.html.erb

改变

  <%= link_to 'Delete comment', [comment.article, comment],
          method: :delete,
          data: {confirm: 'Are you sure you want to delete this comment?'} %>

  <%= link_to 'Delete comment', comment_path(comment),
          method: :delete,
          data: {confirm: 'Are you sure you want to delete this comment?'} %>

发生这种情况的原因是这一行:

<%= form_with(model: [@article, @article.comments.build], local: true) do |form| %>

@article.comments.build 的部分正在对文章建立空评论。如果文章没有评论,而您要打印出 @article.comments.count,则该评论将为零。它这样做是因为 @article.comments.count 运行查询,并且由于尚未保存空白评论,因此它不计入评论数。

作为旁注,@article.comments.size 将 return 1,因为在这种情况下它 return 是与空白注释的关系的大小。这就是为什么当文章没有评论时你不会得到空白评论的原因。

但是,如果您已经有评论并打印出来 @article.comments.count,那么它将是 1,因为现在您在数据库中保存了一条评论。这会在页面上呈现您的评论。问题是 @article.comments return 值中有一个空白注释。这被打印到屏幕上,并且由于它没有 id,删除的路由会像这样 /article/11/comments 呈现,没有评论 id。这条路线不存在,所以你得到一个错误。

解决此问题的一种可能方法是更改​​ comment_section 部分中的这一行:

<%= render @article.comments %>

对此:

<%= render @article.comments.select { |comment| comment.persisted? %>

更新:

我认为 arieljuod 的解决方案更干净,要更改它:

<%= form_with(model: [@article, @article.comments.build], local: true) do |form| %>

为此:

<%= form_with(model: [@article, Comment.new], local: true) do |form| %>