[英]Grails - Domain Record Not Updating on .Save()
长期聆听者,第一次来电者。 我已经练习编码大约 1 年了,我目前正在尝试在 Grails 中构建一个小型 CRUD 应用程序。 在过去的 3-4 周里,我无法更新 Grails 随附的 h2 数据库中的记录。 我在网上阅读了很多文章,通过 Stack Overflow 进行了筛选,甚至购买了《Grails in Action》一书作为参考。 每个资源都让我相信这应该相对容易。 我不确定我的问题是否在我看来(可能是我如何发布数据),在我的 controller 中,或者可能在我的 application.yml 中。 下面是我的 HTML、javascript、controller、域和 application.yml 代码。 任何帮助是极大的赞赏。
需要注意的几点: 1) 执行更新时,Grails 验证为零错误。 2) 更新操作将对象的新属性打印到控制台,但数据库没有更新。
HTML - 注意,javascript function 用于将只读属性从 true 更改为 false
<form method="POST" action="../update">
<!--hidden field for contractor id-->
<input name="id" value="${params.id}" hidden>
<div class="row">
<div class="col-15">
<label for="name">Name:</label>
</div>
<div class="col-40">
<input class="readOnly" type="text" id="name" name="name" value="${params.name}" readonly>
</div>
<div class="editIcon">
<i id="editIcon" class="far fa-edit" title="Edit Contractor"></i>
</div>
</div>
<div class="row">
<div class="col-15">
<label for="street">Street:</label>
</div>
<div class="col-40">
<input class="readOnly" type="text" id="street" name="street" value="${params.street}" readonly>
</div>
</div>
<div class="row">
<div class="col-15">
<label for="city">City:</label>
</div>
<div class="col-40">
<input class="readOnly" type="text" id="city" name="city" value="${params.city}" readonly>
</div>
</div>
<div class="row">
<div class="col-15">
<label for="state">State:</label>
</div>
<div class="col-15 removeLeftPadding">
<select id="state" name="state" disabled>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
</select>
</div>
<div class="col-05 zip">
<label for="zip">Zip:</label>
</div>
<div class="col-13">
<input class="readOnly" type="text" id="zip" name="zip" value="${params.zip}" readonly>
</div>
</div>
<div class="row">
<div class="col-15">
<label for="paymentType">Payment Type:</label>
</div>
<div>
<select id="paymentType" name="paymentType" disabled>
<option value="1">ACH</option>
<option value="2">Credit Card</option>
</select>
</div>
</div>
<div class="row">
<input type="submit" value="Update">
</div>
</form>
JavaScript - 我怀疑问题出在此处,因为在 Google 开发工具中查看网络选项卡时传递了参数(请参见下面的屏幕截图)。
/*When icon edit icon is clicked, set readOnly/disabled values to false and input/icon colors to appropriate colors*/
$( "#editIcon" ).click(function() {
console.log("Edit called.")
if(!clicked){
$( "#name" ).prop('readonly', false)
$( "#street" ).prop('readonly', false)
$( "#city" ).prop('readonly', false)
$( "#state" ).prop('disabled', false)
$( "#zip" ).prop('readonly', false)
$( "#paymentType" ).prop('disabled', false)
//Set input color to black
$( "#name" ).css("color", "black")
$( "#street" ).css("color", "black")
$( "#city" ).css("color", "black")
$( "#state" ).css("color", "black")
$( "#zip" ).css("color", "black")
$( "#paymentType" ).css("color", "black")
//Set icon to red
$("#editIcon").css("color", "red")
clicked = true;
} else{
$( "#name" ).prop('readonly', true)
$( "#street" ).prop('readonly', true)
$( "#city" ).prop('readonly', true)
$( "#state" ).prop('disabled', true)
$( "#zip" ).prop('readonly', true)
$( "#paymentType" ).prop('disabled', true)
//Set input color to black
$( "#name" ).css("color", "gray")
$( "#street" ).css("color", "gray")
$( "#city" ).css("color", "gray")
$( "#state" ).css("color", "gray")
$( "#zip" ).css("color", "gray")
$( "#paymentType" ).css("color", "gray")
//Set icon to red
$("#editIcon").css("color", "#7543b7")
clicked = false;
}
});
承包商 Controller 中的更新操作
def update(Long id){
println("I made it to update for Contractors")
def contractorInstance = Contractor.get(id)
contractorInstance.properties = params
println contractorInstance.validate()
println contractorInstance.errors
if(!contractorInstance.save()){
println 'Contractor did not update.'
} else{
println 'Contractor updated.'
}
println contractorInstance.id //prints id to console
println contractorInstance.name //prints updated name to console
redirect(action: "index")
}
Application.yml 中的开发数据源
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
logsql: true #added by PCB on 5/14
承包商域
class Contractor {
String name
String street
String city
String state
String zip
PaymentType paymentType
static hasMany = [masteragreements: MasterAgreement, contracts: Contract, termsAndConditions: TermsAndConditions]
static constraints = {
}
}
编辑:我正在使用 Grails 版本 4.0.3
在 Grails 4/Hibernate 5.2+ 中写入数据库需要一个事务。 在您的情况下,您正在写入 session 但从未指示将 session 刷新(写入)到数据库。 这发生在事务结束或显式调用刷新时。
最常见的解决方案是将逻辑移动到服务并使用@Transactional
或使用 GORM 数据服务为您管理事务时对方法进行注释。
您可以强制刷新(并查看您的错误)的一种方法是执行以下操作:
if(!instance.save(flush: true)){
instance.errors.allErrors.each { println it }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.