在 ajax rails 表单后未显示验证错误

Validation errors not showing after I ajax a rails form

我正在使用 ajax-rails 呈现我的表单和验证停止工作。当我点击提交应该验证的空时,验证不生效并且后端日志显示它发布了空表单。如果我不使用 ajax 它工作正常。我不知道我错过了什么。

型号

class ClockEntry < ApplicationRecord
  belongs_to :user

  validates :purpose, presence: true # I validated the attribute
end

index.html.erb

<div class="container" id="new-clock-entry">
  <%= link_to 'New Clock Entry', new_clock_entry_path, remote: true, class: 'btn btn-primary btn-sm' %>
</div>

_form.html.erb

<%= simple_form_for clock_entry, remote: true do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%= f.input :purpose %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, class: 'btn btn-primary btn-block btn-lg' %>
  </div>
<% end %>

new.html.erb

<h1>New Clock Entry</h1>

<%= render 'form', clock_entry: @clock_entry %>

<%= link_to 'Back', clock_entries_path %>

new.js.erb

$('#new-clock-entry a').hide().parent().append("<%= j render 'form', clock_entry: @clock_entry %>")

create.js.erb

$('#new-clock-entry form').remove();
$('#new-clock-entry a').show();
$('table#clock-entry tbody').append("<%= j render @clock_entry %>");

控制器

def new
  @clock_entry = ClockEntry.new
end

def create
    @clock_entry = current_user.clock_entries.new(clock_entry_params)
    @clock_entry.set_time_in

    respond_to do |format|
      if @clock_entry.save
        format.js { redirect_to root_path, notice: 'Clock entry was successfully created.' }
        format.html { redirect_to @clock_entry, notice: 'Clock entry was successfully created.' }
        format.json { render :show, status: :created, location: @clock_entry }
      else
        format.html { render :new }
        format.json { render json: @clock_entry.errors, status: :unprocessable_entity }
      end
    end
  end

评论区的@arieljuod 对我解决这个问题很有帮助。正如他首先提到的,我不是在创建方法的 else 条件下询问我的 format to respond to js。所以这就是我所做的:

控制器创建操作

将以下行添加到创建操作的 else 条件中:

format.js { render :new }

所以我的控制器动作变成:

def create
    @clock_entry = current_user.clock_entries.new(clock_entry_params)
    @clock_entry.set_time_in

    respond_to do |format|
      if @clock_entry.save
        format.html { redirect_to @clock_entry, notice: 'Clock entry was successfully created.' }
        format.js { redirect_to root_path, notice: 'Clock entry was successfully created.' }
        format.json { render :show, status: :created, location: @clock_entry }
      else
        format.js { render :new } # Added this...
        format.html { render :new }
        format.json { render json: @clock_entry.errors, status: :unprocessable_entity }
      end
    end
  end

new.js.erb 文件

然后在 new.js.erb 文件中,在呈现 :new 表单时,您需要删除或隐藏已经存在的内容并附加一个包含错误消息的新表单。所以我不得不通过在 new.js.erb 中提供 form tag to be hidden 来删除整个表格。所以我将下面这行添加到我的 new.js.erb 文件中:

$('#new-clock-entry form').hide().parent().append("<%= j render 'form', clock_entry: @clock_entry %>")

所以新的 new.js.erb 文件现在变成:

$('#new-clock-entry a').hide().parent().append("<%= j render 'form', clock_entry: @clock_entry %>")
$('#new-clock-entry form').hide().parent().append("<%= j render 'form', clock_entry: @clock_entry %>")

我认为这对遇到相同问题的任何人都应该很方便。