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
in erb, something like “<% if comment.body.empty? %> Your error message <% end %>” should sort you… If not, please give further information – H
First, you can have a basic validation via JS adding to that body input the attribute required: true. That will trigger HTML5 validation if you try tu submit a empty input.
If you have your validation via ActiveRecord in your model, you need tu put specific code in your view to show the error. Are you looking for @your_record.errors ?
You can see the error messages triggered by your validation (on the model) looking at @your_record.errors.full_messages
First thing to check is whether your validations are setting the error state on your form object as is normal:
<%= form_with model: @foo do |f| %>
<%= f.text_field :bar %>
<%= f.submit %>
That's a normal .erb form for a mythical object @foo:
class Foo < ApplicationRecord
validates :bar, presence: true
end
If you try to submit this form in a browser without a value in the foo[bar] field, you will see that the resulting re-display of the :new view will have a div.field_with_errors wrapped around the input[type="text"]#foo_bar element. This class field_with_errors can be styled so that you see red text etc, pointing out where the problem exists. The @foo.errors[:full_messages] struct will contain a key-value dictionary of all the fields that have errors and a human-readable error message for each one. These messages are generated by the validations framework, and can be customized using the I18n translations system.
If you build a new dummy Rails application, and use the following command within it to create a "scaffold" for Foo, you can see this in action, along with some code that will also display a summary of errors at the top of the form page:
rails generate scaffold Foo bar:string
Edit the generated app/models/foo.rb to add the `validates` line as above.
Running this app with rails server and viewing it in a browser at localhost:3000/foos/new will show you a form, and if you submit it without entering anything, you should see the error messages. Look in the view as well as the controller to see how this is managed.
Many form helpers exist to allow you to use inline error messages (which appear in context) rather than all-together-at-the-top errors lists. Personally, I find these a bit more useable, particularly on a tall form. I really like GitHub - bootstrap-ruby/bootstrap_form: Official repository of the bootstrap_form gem, a Rails form builder that makes it super easy to create beautiful-looking forms using Bootstrap 5. for this.
Walter
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
5
6 <%= pluralize(@article.errors.count, “error”) %> prohibited this article from being saved:
7
8
9 <% @article.errors.full_messages.each do |msg| %>
10 - <%= msg %>
11 <% end %>
12
13 <% end %>
14
15
16 <%= form.label :commenter %>
17 <%= form.text_field :commenter %>
18
19
20 <%= form.label :body %>
21 <%= form.text_area :body %>
22
23
24 <%= form.submit %>
25
26 <% end %>