Rails Simple Form - Add an error not related to attribute
<p>I wanna do a validation that sums the values of nested fields so I make sure it's 100%. So, in my parent model, I would do the validation, and do a <code>self.errors.add</code> and add the error if the validation fails. The problem is, the <code>errors.add</code> as long as I know expects some attribute as argument, but it's not related to any attribute on my parent model, so I would like to display that message on the top of the form, for example. Any ideas of how I can do that? thank you!</p>

<p>UPDATE:</p>

<p>This is my parent model, where I wanna validate. The form has nested fields for <code>:arrendamento_contrato_unidades.</code></p>

<pre><code>    class ArrendamentoContrato &lt; ApplicationRecord

      has_many :arrendamento_contrato_unidades, dependent: :destroy

      validate :check_total_percentual_credito


  def check_total_percentual_credito
    if arrendamento_contrato_unidades.sum(&amp;:percentual_credito).to_f != 100.0
      self.errors.add :base, "Tem que ser 100%"
    end
  end


    end
</code></pre>

<p>My create method, which it's the one I'm testing:</p>

<pre><code>  def create


    @arrendamento_contrato = ArrendamentoContrato.new(arrendamento_contrato_params)

    respond_to do |format|
      if @arrendamento_contrato.save

        format.html { 
          flash[:notice] = flash_notice
          redirect_to action: "index"
        }
        format.json { render json: {status: 1, redirect: arrendamento_contratos_url} }
      else
        format.html { render :new }
        format.json { render json: {status: 0, errors: @arrendamento_contrato.errors, status: :unprocessable_entity} }
      end
    end
  end
</code></pre>

<p>--- Also, I debugged my object.errors.full_messages on the form, and the error is there. It's only not being displayed!</p>

<p>I guess that add errors to the base it's what I'm looking for. But now, it's not showing my message, but only that I have validation errors. My form code:</p>

<pre><code>= simple_form_for(@arrendamento_contrato, validate: true, html: { id:"dropzoneForm", class: "dropzone dz-clickable"}) do |f|
  = f.error_notification

  .form-inputs

    .row

      .col-md-6

        = f.input :numero

      .col-md-6

        = f.association :usina, input_html: {class: "chosen-select"}             

    .hr-line-dashed

    .row

      .col-md-6

        = f.association :esco_contrato, input_html: {class: "chosen-select"}

      .col-md-6

        = f.association :om_contrato, input_html: {class: "chosen-select"}


    .hr-line-dashed

    .row

      .col-md-4
        = f.input :data_inicio, as: :string, input_html: {"data-mask" =&gt; "date"} 

      .col-md-4
        = f.input :data_fim, as: :string, input_html: {"data-mask" =&gt; "date"} 

      .col-md-4
        = f.input :valor_mensal, as: :string, input_html: {"data-mask" =&gt; "decimal"}


    .hr-line-dashed        

    #arrendamento_contratos_unidades
      - if !@arrendamento_contrato.arrendamento_contrato_unidades || @arrendamento_contrato.arrendamento_contrato_unidades.empty?
        h3 = I18n.t('activerecord.models.unidade_consumidora.other')
        i
          'Aguardando ESCO...
      - else
        .row
          .col-md-6
            label class='control-label'
              = I18n.t('activerecord.models.unidade_consumidora.other')
          .col-md-6
            label class='control-label'
              = I18n.t('activerecord.attributes.arrendamento_contrato_unidade.percentual_credito')
        .hr-line-dashed
        .blockquote
          = f.simple_fields_for :arrendamento_contrato_unidades do |f|
            = render 'arrendamento_contrato_unidade_fields', f: f

    .hr-line-dashed
</code></pre>