【发布时间】:2021-04-04 10:26:06
【问题描述】:
我无法弄清楚我在 edit.js.slim 文件中做错了什么。当必填字段为空时,它不显示验证。
控制器
# frozen_string_literal: true
class MerchantsController < ApplicationController
include ControllerHelpers::StrongParameters
def index
@merchants = Merchant.all.load
end
def edit
merchant
end
def update
merchant
respond_to do |format|
if @merchant.update(update_params)
format.js { redirect_to merchants_path, notice: 'Merchant was successfully updated.' }
format.json { render :show, status: :ok, location: @merchant }
else
format.js { render :edit }
format.json { render json: @merchant.errors, status: :unprocessable_entity }
end
end
end
private
def merchant
@merchant ||= Merchant.find(params[:id])
end
def update_params
params.require(:merchant).permit(permitted_merchant_update_attributes)
end
end
edit.js.slim # 这不会在表单上显示验证错误,即使我点击 700 次
| $('.divTable.blueTable').bind('ajax:success', function () { $(this).hide().parent().append("
= j render 'inline_edit_form', merchant: @merchant
| "); })
_inline_edit_form.html.slim
= simple_form_for @merchant, id: dom_id(@merchant, 'inline_edit_form'), remote: true do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
.divTable.blueTable
.divTableHeading
.divTableRow
.divTableHead
| Name
.divTableHead
| Description
.divTableHead
| Email
.divTableHead
| Status
.divTableHead[colspan="2"]
| Actions
.divTableBody
.divTableCell
= f.input :name, label: false
.divTableCell
= f.input :description, label: false
.divTableCell
= f.input :email, label: false
.divTableCell
= f.input :status, label: false
.divTableCell
= f.button :submit, class: 'btn btn-primary btn-block btn-sm'
update.js.slim
| $('.refresh').bind('ajax:success', function () { $(this).hide().parent(); })
页面来源
所以在尝试解决它时,我在edit.js.slim 中提出了另一个解决方案,它显示了验证,但是当我第二次单击更新按钮时,它重定向到响应 HTML 的 marchant_path,而不是保留在表单上并显示验证违规。
| $('.divTable.blueTable').replaceWith('
= j render 'inline_edit_form'
| ');
注意:我使用的是 Rails 6.1.0
【问题讨论】:
标签: javascript jquery ruby-on-rails ruby ajax