Comment in the Blog test project

10 views
Skip to first unread message

jason cao

unread,
Dec 26, 2018, 5:58:32 AM12/26/18
to Ruby on Rails: Talk
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

Hasan Diwan

unread,
Dec 26, 2018, 6:00:59 AM12/26/18
to rubyonra...@googlegroups.com
in erb, something like "<% if comment.body.empty? %> Your error message <% end %>" should sort you... If not, please give further information -- H

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2abe97f7-2e6b-452b-85d9-a9980d6982fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
If you wish to request my time, please do so using bit.ly/hd1AppointmentRequest.
Si vous voudrais faire connnaisance, allez a bit.ly/hd1AppointmentRequest.

Sent from my mobile device
Envoye de mon portable

Franco Cravero Mendiburú

unread,
Dec 26, 2018, 10:42:18 AM12/26/18
to Ruby on Rails: Talk
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

Walter Lee Davis

unread,
Dec 26, 2018, 1:35:09 PM12/26/18
to rubyonra...@googlegroups.com
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 https://github.com/bootstrap-ruby/bootstrap_form for this.

Walter

fugee ohu

unread,
Dec 26, 2018, 5:06:08 PM12/26/18
to Ruby on Rails: Talk
if @object.errors.any?

cg jieweihui

unread,
Dec 27, 2018, 3:26:55 AM12/27/18
to rubyonra...@googlegroups.com
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

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages