Format and basic errors

Formtastic docs talk about adding a line for basic errors:

<%= semantic_form_for @record do |form| %> <%= form.semantic_errors :base %> ...main body of form... <% end %> 

What puzzles me is that Formtastic will include easy support for field-related errors, but by default it does not offer such places for base . 1) Do you know what are the reasons for this?

2) In my situation, I would like all my forms to include basic errors immediately after the start of the form. Is there a way for me to get formtastic to do this by default for all forms?

+7
source share
2 answers

If you want to display all basic errors along with any and all errors of nested attributes:

f.semantic_errors *f.object.errors.keys

From: https://github.com/gregbell/active_admin/pull/905

+12
source

In Formtastic 2.x, semantic_errors ALWAYS includes: base.

However, I noticed in Rails 3 (I'm in Rails 3.2) that error messages from validation are no longer stored on the database, but stored by attribute. For example, with this class:

 class User < ActiveRecord::Base # has a name attribute validates :name, :presence => true, :uniqueness => true end 

Your @ user.errors object when creating / updating fails will look like this:

 #<ActiveModel::Errors:0x0000000 @base=#<User id:1, name: "">, @messages={:name => ["can't be blank"]}> 

If you want to make your form as follows, it will throw an error for the name attribute.

 <%= semantic_form_for @user do |form| %> <%= form.semantic_errors :name %> ...main body of form... <% end %> 

This is an example layout, but you understand what I mean.

Unfortunately, there is currently nothing in formtastic codebase that supports the "all" option with Rails 3 (again, I am using Rails 3.2)

+3
source

All Articles