长期听众,第一次打电话。我练习编码已经有~1年了,目前正在尝试用Grails构建一个小型CRUD应用。在过去的3-4周里,我一直无法更新Grails自带的h2数据库中的记录。我在网上阅读了很多文章,在Stack Overflow中进行了筛选,甚至购买了《Grails in Action》一书作为参考。每一份资料都让我相信这应该是比较容易的。我不知道我的问题是在我的视图中(也许是我发布数据的方式),还是在我的控制器中,或者可能在我的application.yml中。下面是我的HTML、javascript、控制器、域名和application.yml代码。任何帮助都是非常感激的。
需要注意的是:1)当更新被执行时,Grails的验证是零错误的。2)更新动作将对象的新属性打印到控制台,但数据库没有更新。
HTML--注意,javascript函数用于将readOnly属性从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;
}
});
承包商控制器中的更新动作
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 = {
}
}
EDIT:我用的是Grails 4.0.3版。
在Grails 4Hibernate 5.2+中,需要一个Transaction来写入数据库。在你的情况下,你是在向一个会话写入,但从未指示该会话被刷新(写入)到数据库。这发生在事务结束或显式调用flush的时候。
最常见的解决方案是将逻辑移到一个Service中,并在方法中注解了 @Transactional
或使用GORM数据服务,因为他们为你管理交易。
你可以强制刷新(并查看你的错误)的方法之一是这样做。
if(!instance.save(flush: true)){
instance.errors.allErrors.each { println it }
}