I am creating a posting site as a portfolio. When implementing the comment function, a Missing Template error occurred in the part called by the partial template, so I will write about the solution.
Implemented comment posting function on the article detail page
If you write a comment in the comment form and press the submit button, it will be saved correctly, but if you press the submit button while the comment form is blank, a Missing Template error will occur in the partial template part as shown below.
The code related to the view controller when an error occurs is as follows.
Display the content of the article by calling _article.html.erb that was cut out from the partial template, and implement the comment form under it.
HTML:show.html.erb
<div class="show-main">
<div class="show-post">
<%= render partial: "article", locals: {article: @article}%>
</div>
<div class="comment-form">
<%= form_with model: [@article, @comment], url: article_comments_path, local: true do |f| %>
<%= render 'shared/error_messages', model: f.object %>
<div class="comment-main">
<%= f.text_area :message, placeholder: "To comment", class: "comment-text" %>
<%= f.submit 'Send', class: "comment-btn" %>
</div>
<% end %>
</div>
</div>
The comments controller and articles controller are as follows. Display the comment form with articles # show and save the comment with comments # create. Also, in comments # create, enter a conditional branch when saving comments succeeds and when it fails.
comments_controller.rb
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.new(comment_params)
@comments = @article.comments.includes(:user)
if @comment.save
redirect_to article_path(@article)
else
render template: "articles/show"
end
end
private
def comment_params
params.require(:comment).permit(:message).merge(user_id: current_user.id, article_id: params[:article_id] )
end
end
articles_controller.rb
class ArticlesController < ApplicationController
#Omission
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comments = @article.comments.includes(:user)
end
end
Since the error occurs only when the comment is blank, review the rendering part. As a result, the error was resolved by changing the URL of the partial template from "article" to "articles/articles". This is due to the following relationship when calling the _article.html.erb file. -Partial template call from view of articles # show → Since the call is made from within the articles folder, "article" is sufficient to specify the URL. -Partial template call when accessing articles # show from comments controller (When comment posting fails) → Because it is in the comments folder, it is necessary to specify the URL of "articles/articles" even higher. In addition, the error did not occur when the comment was posted correctly because redirect \ _to was used to get the path when the comment was posted successfully, and the page transition was performed through routing.
Part of show.html.erb Change the URL of the template part from "article" to "articles/article"
HTML:show.html.erb
<div class="show-main">
<div class="show-post">
<%= render partial: "articles/article", locals: {article: @article}%>
</div>
<div class="comment-form">
<%= form_with model: [@article, @comment], url: article_comments_path, local: true do |f| %>
<%= render 'shared/error_messages', model: f.object %>
<div class="comment-main">
<%= f.text_area :message, placeholder: "To comment", class: "comment-text" %>
<%= f.submit 'Send', class: "comment-btn" %>
</div>
<% end %>
</div>
</div>
In the comments controller, I wondered why the rendering path specified "articles/show" one level higher, so I couldn't implement it correctly, so I struggled with this error. As a result, I think the main reason is that I didn't understand the part that the request is sent directly from the controller to the view when rendering. Like me, I hope this article helps beginners when implementing the commenting feature.
・ About the difference between redirect \ _to and render https://qiita.com/morikuma709/items/e9146465df2d8a094d78
Recommended Posts