How to resolve Missing Template error when implementing comment function

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.

Features you want to implement

Implemented comment posting function on the article detail page

Error details

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. image.png

code

The code related to the view controller when an error occurs is as follows.

View

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>

controller

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

Causes of errors and their solutions

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.

Completion code

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>

Summary

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.

reference

・ About the difference between redirect \ _to and render https://qiita.com/morikuma709/items/e9146465df2d8a094d78

Recommended Posts

How to resolve Missing Template error when implementing comment function
[Rails] How to display error messages for comment function (for beginners)
How to resolve errors when installing Rails 5.1.3
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
How to resolve SSL_connect error in PayPal Ruby SDK
How to resolve errors when doing git push heroku master
How to resolve Sprockets :: DoubleLinkError
How to resolve Git conflicts
How to add ActionText function
How to leave a comment
[Ruby] How to comment out
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
How to solve the unknown error when using slf4j in Java
[Rails] PG :: DuplicateTable: ERROR: relation "users" How to resolve already exists
About error handling of comment function
How to resolve Mixed Content errors
[Technical memo] How to resolve errors
How to add the delete function
About error when implementing spring validation
[Docker] How to solve the error function not implemented @ io_fread ~ [Super easy]
What to do and how to install when an error occurs in DXRuby 1.4.7
How to display error messages and success messages when registering as a user
[Java] How to use the hasNext function
Error when trying to use heroku command
[Rails] How to display error messages individually
Error in implementation when implementing Spring validation
[Swift] How to implement the countdown function
How to implement TextInputLayout with validation function
[Servlet / Ajax] How to resolve NoClassDefFoundError [Eclipse]
[rails] How to create a partial template
[Processing × Java] How to use the function
How to display error messages in Japanese
[Solution] Webpacker error when introducing Vuetify to Rails6 "ActionView :: Template :: Error Webpacker can't find hello_vue ~"
How to resolve the "OpenSSH keys only supported if ED25519 is available" error
[Laravel] How to deal with out of memory error when composer require [Docker]
[Error] How to resolve the event that the screen does not transition after editing
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
[Heroku] How to solve when an error is displayed on git push heroku master