如何解决出现在ERB生成的HTML中的额外数据

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

我正在按照ruby-on-rails说明指南来创建简单的blog Web应用程序: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?'}
  %>

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

Empty comment section

但是,当显示带有一些实际注释的文章时,最后会显示一个多余的空注释:

Single comment

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

Deletion error

删除其他评论很好。

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

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上进行开发

html ruby-on-rails ruby erb
2个回答
0
投票

在您的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?'} %>

to

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

0
投票

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

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

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

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

但是,如果您已经有注释并打印出@article.comments.count,它将为1,因为现在您在数据库中有一个保存的注释。现在,您的评论将显示在页面上。问题是@article.comments返回值内有一个空白注释。这将被打印到屏幕上,并且由于它没有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| %>

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