Thanks all for your reply. I understand the method you mentioned, but it
doesn't work for this case (Comment is an associated class with Blog). Here
is the code of _form.html.erb in views/comments folder:

  1
  2 <%= form_with(model: [@article, @article.comments.build], local: true)
do |form| %>
  3   <% if @article.errors.any? %>
  4     <div id="error_explanation">
  5       <h2>
  6         <%= pluralize(@article.errors.count, "error") %> prohibited
this article from being saved:
  7       </h2>
  8       <ul>
  9               <% @article.errors.full_messages.each do |msg| %>
 10                       <li>    <%= msg %> </li>
 11               <% end %>
 12       </ul>
 13   <% end %>
 14
 15         <p>
 16                 <%= form.label :commenter %><br>
 17                 <%= form.text_field :commenter %>
 18         </p>
 19         <p>
 20                 <%= form.label :body %><br>
 21                 <%= form.text_area :body %>
 22         </p>
 23         <p>
 24                 <%= form.submit %>
 25         </p>
 26 <% end %>
~

~
Please let me know if you need more information.

Thanks and Regards,
Jason

On Thu, Dec 27, 2018 at 6:06 AM fugee ohu <fugee...@gmail.com> wrote:

> if @object.errors.any?
>
> On Wednesday, December 26, 2018 at 5:58:32 AM UTC-5, jason cao wrote:
>>
>> Hi,
>>
>> How can I show error messages if a comment is invalid? E.g. when a
>> comment body is empty. I add validation in the model class, and the
>> validation itself works, but it won't show any error message. Can you
>> please help? Many thanks!
>>
>> Regards,
>> Jason
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/22f1eb0d-6af1-440a-8aff-e4c539bf1a37%40googlegroups.com
> <https://groups.google.com/d/msgid/rubyonrails-talk/22f1eb0d-6af1-440a-8aff-e4c539bf1a37%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CALeWamRkNJhcxniBGLYQXOnUWh61FOAwNkak4eVg%3DBE%3D5Y%3DVTw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to