因为我对Ruby很陌生。在运行localhost URL之后,我将得到这个错误。为了这条线。它位于show.html.erb文件中。
<%= render @article.comments %>
Showing D:/RailProject/App/app/views/articles/show.html.erb where line #14 raised:
Missing partial comments/_comment with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.
Searched in:
* "D:/RailProject/App/app/views"
* "C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/actiontext-7.0.3/app/views"
* "C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/actionmailbox-7.0.3/app/views"我在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), status: 303
end
def show
@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, :status)
end end我在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>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>我有_comments.html.erb:
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to "Destroy Comment", [comment.article, comment], data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
</p>发布于 2022-06-22 14:08:43
您需要创建一个名为app/views/comments/_comment.html.erb的文件,这样Rails就可以使用它来呈现每个注释。这就是您在编写<%= render @article.comments %>时要求它做的事情。对于传递给render的集合中的每个元素,Rails将尝试呈现一个分部,到该部分的路径是从模型的名称派生出来的。
如果@article.comments包含3个If为1, 2, 3的记录,如果您的app/views/comments/_comment.html.erb包含以下内容.
<h1>Comment <%= comment.id %></h1>然后Rails将对每个注释进行三次呈现,并生成以下内容:
<h1>Comment 1</h1>
<h1>Comment 2</h1>
<h1>Comment 3</h1>https://stackoverflow.com/questions/72717028
复制相似问题