← Back to Upcase

Nested Form Validations


(Brian Dear) #1

I can’t seem to get my error messages partials to show up for a nested form.

Here’s the specifics:

user.rb

...
  validates :email, presence: { message: 'Please enter your email'}
  validates :email, uniqueness: { case_sensitive: false,
                                   message: 'Someone is already using that
                                   email address. Please try another.' }
...
  belongs_to :account

account.rb

...
  validates :name, presence: true

  belongs_to :owner, class_name: 'User'
  accepts_nested_attributes_for :owner
  has_many :users, dependent: :destroy
...

accounts/new.html.haml
note: using decent_exposure

= form_for account do |a|
  - if account.errors.any?
    #errorExplanation
      %ul
        - account.owner.errors.full_messages.each do |msg|
          %li
            = msg

  .field.text
    = a.label :name, 'Company name'
    = a.text_field :name

  = a.fields_for :owner do |o|
    = render 'users/form', user: o

  .actions
    = a.submit 'Sign up'

I can’t understand why the errors aren’t showing up. They ARE showing up in the console however, meaning if I do a binding.pry and check account.owner.errors, the correct error messages are happening.