RailsGuides - Getting Started with Rails. Add Comment validation

Hi everyone. I’m new to Rails so trying to read all the documentation.

While reading through the Getting Started with Rails I’ve noticed that there is no section about article Comments validation, but I think it would be great to have such chapter. It is hard for me to make it up for now, so I would appreciate any guidance on how to create Comment validation(mandatory author and body) and show errors of invalid submission to the user.
I’m looking for a way to do something like you suggest in Article validation, I’ve tried to modify it in some ways, but it didn’t work.
Thank you for any help.

Hi there! Here are two options that I try on how to validate comment inside an article based on Ruby on Rails guides.

  1. Slightly elegant way
# comments_controller.rb

def create
     @article = Article.find(params[:article_id])
-       @comment = @article.comments.create(comment_params)
+     @comment = @article.comments.new(comment_params)

+     unless @comment.valid?
+       @commenter_errors = @comment.errors.full_messages_for(:commenter)
+      @body_errors = @comment.errors.full_messages_for(:body)
+     end

+     if @comment.save
       redirect_to article_path(@article)
+    else
+      render 'articles/show', status: :unprocessable_entity
    end
  end
end
# app/views/articles/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>Add a comment:</h2>
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
+ <% if @commenter_errors.present? %>
+            <div class="errors">
+           <% @commenter_errors.each do |error| %>
+              <p class="error-message"><%= error %></p>
+         <% end %>
+             </div>
+         <% end %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
+ <% if @body_errors.present? %>
+            <div class="errors">
+           <% @body_errors.each do |error| %>
+              <p class="error-message"><%= error %></p>
+         <% end %>
+             </div>
+         <% end %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>
  1. Elegant way like article does.
# articles_controller.rb
def show
     @article = Article.find(params[:id])
+   @comment = Comment.new(article: @arcticle)
end
# app/views/articles/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>Add a comment:</h2>
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
+ <% if @comment.errors.any? %>
+         <% @comment.errors.full_messages_for(:commenter).each do |message| %>
+                 <div>
+                     <%= message %>
+                 </div>
+         <% end %>
+  <% end %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
+ <% if @comment.errors.any? %>
+         <% @comment.errors.full_messages_for(:body).each do |message| %>
+                 <div>
+                     <%= message %>
+                 </div>
+         <% end %>
+  <% end %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>

We use @comment = Comment.new(article: @article) instead of @comment = Comment.new in articles_controller.rb because we are going to add comment that associate to an article. I hope it helps anyone who try Ruby on Rails in Ruby on Rails guides.

PS: I just try Ruby and Ruby on Rails around two weeks and I’m still not fluently to use these tools. I’m using GitHub Copilot to assist me to solve this problem.

2 Likes

Glad to see someone using Copilot! I am trying to learn rails using codespaces and copilot.

1 Like