Rails中的嵌套表单验证

时间:2018-01-29 19:24:41

标签: ruby-on-rails

我有以下代码:

<%= form_with(model: [@lawsuit, @lawsuit.suits.build]) do |f| %>

  <fieldset>
    <legend>New Suit</legend>
  </fieldset>
  <br />
  <div class="form-group">
    <%= f.label :claim %>
    <%= f.text_field :claim, class: 'form-control', placeholder: 'Name' %>
  </div>
  <div class="form-group">
    <%= f.label :sentence %>
    <%= f.text_area :sentence, class: 'form-control', placeholder: 'Sentence' %>
  </div>
  <div class="form-group">
    <%= f.label :result %>
    <%= f.select(:result, [['Not Favorable', false], ['Favorable', true]], {}, {class: 'form-control'}) %>
  </div>

  <%= button_tag type: 'submit', class: "btn btn-primary float-right" do %>
    <i class="fa fa-plus" aria-hidden="true"></i>
    Create
  <% end %>
<% end %>

如何显示套装错误列表(@lawsuit的嵌套属性)并在屏幕上显示错误?我已经对模型进行了验证。模型就是这样:

class Suit < ApplicationRecord
  belongs_to :lawsuit

  validates_presence_of :claim, :sentence
end

我的控制器如下所示。

该过程从诉讼控制器开始。在那里我构建了@suit(在表单中使用)。

西装控制器:

class SuitsController < 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

诉讼控制员

class LawsuitsController < ApplicationController
    before_action :set_lawsuit, only: [:show]

  def index
      @lawsuits = Lawsuit.paginate(:page => params[:page], :per_page => 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

1 个答案:

答案 0 :(得分:-1)

修改视图如下

<%= form_with(model: [:lawsuit, @suit]) do |f| %>

  <fieldset>
    <legend>New Suit</legend>
  </fieldset>
  <br />

  <% if @suit.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@suit.errors.count, "error") %>
        <%= "prohibited this suit from being saved:" %>
      </h2>
      <ul></ul>
      <% @suit.errors.full_messages.each do |message| %>
        <li>
          <%= message %>
        </li>
      <% end %>
    </div>
  <% end %>


  <div class="form-group">
    <%= f.label :claim %>
    <%= f.text_field :claim, class: 'form-control', placeholder: 'Name' %>
  </div>
  <div class="form-group">
    <%= f.label :sentence %>
    <%= f.text_area :sentence, class: 'form-control', placeholder: 'Sentence' %>
  </div>
  <div class="form-group">
    <%= f.label :result %>
    <%= f.select(:result, [['Not Favorable', false], ['Favorable', true]], {}, {class: 'form-control'}) %>
  </div>

  <%= button_tag type: 'submit', class: "btn btn-primary float-right" do %>
    <i class="fa fa-plus" aria-hidden="true"></i>
    Create
  <% end %>
<% end %>

请注意,您应首先从控制器操作构建@suit对象

@suit = @lawsuit.suits.build

你的控制器应该是

class SuitsController < ApplicationController
  before_action :set_suit, only: [:show]

  def new
  end

  def create

    @lawsuit = Lawsuit.find(params[:lawsuit_id])
    @suit = @lawsuit.suits.build(suit_params)

    if @suit.save
      flash[:notice] = "Suit created successfully"
      redirect_to lawsuit_path(@lawsuit)
    else
      render :form
    end
  end

如果要显示flash消息,则应在视图中编写以下代码

<% flash.each do |key, value| %>
  <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>