In the Formtastic docs, it talks about adding a line for base errors:
<开发者_如何学Go%= semantic_form_for @record do |form| %>
<%= form.semantic_errors :base %>
...main body of form...
<% end %>
It puzzles me that Formtastic would include effortless support for field-specific errors but by default, it offers no such accommodations for base
. 1) Do you you know what the reasons are for this?
2) In my situation I'd like all my forms to include base errors right after the form begins. Is there a way for me to get formtastic to do this by 'default' for all forms?
If you want to display all base errors along with any and all errors of nested attributes:
f.semantic_errors *f.object.errors.keys
Answer from: https://github.com/gregbell/active_admin/pull/905
In Formtastic 2.x, the semantic_errors ALWAYS includes :base.
However, I have noticed in Rails 3 (I am in Rails 3.2) that error messages from validations are not stored to base anymore, instead 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 on a failed create/update would look like this:
#<ActiveModel::Errors:0x0000000
@base=#<User id:1, name: "">,
@messages={:name => ["can't be blank"]}>
If you were to make your form like this, it would output the error for the :name attribute.
<%= semantic_form_for @user do |form| %>
<%= form.semantic_errors :name %>
...main body of form...
<% end %>
This is a mock example, but you see what I mean.
Unfortunately, there is nothing in the formtastic codebase currently that supports an "all" option with Rails 3 (again, I am using Rails 3.2)
精彩评论