title: Nested form validation in Rails
body: <p>I have the following code:</p>
  
  <pre><code>&lt;%= form_with(model: [@lawsuit, @lawsuit.suits.build]) do |f| %&gt;
  
    &lt;fieldset&gt;
      &lt;legend&gt;New Suit&lt;/legend&gt;
    &lt;/fieldset&gt;
    &lt;br /&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :claim %&gt;
      &lt;%= f.text_field :claim, class: 'form-control', placeholder: 'Name' %&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :sentence %&gt;
      &lt;%= f.text_area :sentence, class: 'form-control', placeholder: 'Sentence' %&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :result %&gt;
      &lt;%= f.select(:result, [['Not Favorable', false], ['Favorable', true]], {}, {class: 'form-control'}) %&gt;
    &lt;/div&gt;
  
    &lt;%= button_tag type: 'submit', class: "btn btn-primary float-right" do %&gt;
      &lt;i class="fa fa-plus" aria-hidden="true"&gt;&lt;/i&gt;
      Create
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>How can I show the list of errors of the suit (which is a nested attribute of @lawsuit) and show it's errors on the screen ? I have already done the validations on the model. The model is like that:</p>
  
  <pre><code>class Suit &lt; ApplicationRecord
    belongs_to :lawsuit
  
    validates_presence_of :claim, :sentence
  end
  </code></pre>
  
  <p>My controllers are like below.            </p>
  
  <p>The process start in lawsuit controller. There I build the @suit (which is used in the form). </p>
  
  <p>Suit controller:</p>
  
  <pre><code>class SuitsController &lt; ApplicationController
    before_action :set_suit, only: [:show]
  
    def new
    end
  
    def create
      Rails.logger.info "=====================SUIT CREATION"
      @lawsuit = Lawsuit.find(params[:lawsuit_id])
      @suit = @lawsuit.suits.build(suit_params)
      Rails.logger.info "AISHA #{@suit.errors.any?}"
      # @suit = @lawsuit.suits.new(suit_params)
  
      if @suit.save
        flash[:notice] = "Suit created successfully"
        redirect_to lawsuit_path(@lawsuit)
      else
        Rails.logger.info "AISHA #{@suit.valid?}"
        flash[:alert] = "Something went wrong"
        redirect_to lawsuit_path(@lawsuit)
      end
    end
  </code></pre>
  
  <p>Lawsuit Controller</p>
  
  <pre><code>class LawsuitsController &lt; ApplicationController
      before_action :set_lawsuit, only: [:show]
  
    def index
        @lawsuits = Lawsuit.paginate(:page =&gt; params[:page], :per_page =&gt; 8)
      end
  
      def show
          begin
              @blob = Lawsuit.get_blob_for_document(@lawsuit.document_number)[1]
          rescue
              @blob = "Cannot load document!"
              flash[:error] = "Cannot load document!"
          end
  
          @lawsuit = Lawsuit.find(params[:id])
          @suit = @lawsuit.suits.build
      end
  
      private
          def set_lawsuit
              @lawsuit = Lawsuit.find(params[:id])
          end
  
          def lawsuit_params
              params.require(:lawsuit).permit(:document_number, :region, :court, :noted)
          end
  end
  </code></pre>