首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails - .Save()上的域记录未更新

Grails - .Save()上的域记录未更新
EN

Stack Overflow用户
提问于 2020-06-05 08:52:04
回答 2查看 280关注 0票数 1

长时间的听众,第一次打电话的人。我已经练习编程大约一年了,现在我正在尝试用Grails构建一个小的CRUD应用程序。在过去的3-4周里,我一直无法更新Grails附带的h2数据库中的记录。我在网上读了很多文章,浏览了Stack Overflow,甚至买了一本名为“Grails in Action”的书作为参考。每个资源都让我相信这应该是相对容易的。我不确定我的问题是在我的视图中(可能是我发布数据的方式),在我的控制器中,还是在我的application.yml中。下面是我的HTML、javascript、控制器、域和application.yml代码。任何帮助都是非常感谢的。

需要注意的几点: 1)当执行更新时,Grails会进行零错误验证。2)更新操作将对象的新属性打印到控制台,但不更新数据库。

注意,javascript函数用于将readOnly属性从true更改为false

代码语言:javascript
复制
 <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 -我怀疑问题在这里,因为在谷歌开发工具中查看网络选项卡时传递了参数(参见下面的屏幕截图)。

代码语言:javascript
复制
    /*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;
    }

});

更新承包商控制器中的操作

代码语言:javascript
复制
  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中开发数据源

代码语言:javascript
复制
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

Dev Tools Screenshot - Form Data Post

承包商域名

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2020-06-05 22:28:17

在Grails 4/Hibernate 5.2+中,需要一个事务来写入数据库。在您的示例中,您正在写入一个会话,但从未指示将该会话刷新(写入)到数据库。这会在事务结束或显式调用flush时发生。

最常见的解决方案是将逻辑移动到服务,并在为您管理事务时使用@Transactional或GORM数据服务对方法进行注释。

票数 2
EN

Stack Overflow用户

发布于 2020-06-06 01:52:20

强制刷新(并查看错误)的方法之一是这样做:

代码语言:javascript
复制
if(!instance.save(flush: true)){
    instance.errors.allErrors.each { println it }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62206415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档