Grails空入数据库 [英] Grails empty entry into the database

查看:134
本文介绍了Grails空入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直努力尝试在Grails中一次创建/保存多个实例,现在我几乎在那里使用下面的代码,但是当我保存时创建了一行空行,任何人都可以用这个$ b来帮助我$ b请看看这两个问题,看看我想达到什么样的目的。

如何使用Grails从一个视图中保存多个对象



Grails一对多关系视图

 < g:textField name =questionvalue =$ {multipleChoiceQuestionInstance?.question}/>< br /> 
< div class =fieldcontain $ {hasErrors(bean:multipleChoiceQuestionInstance,field:'options','error')}>
< label for =options>

< / label>

< ul class =一对多>
< g:set var =countervalue =$ {0}/>
< li>
< g:checkBox name =options [$ {i}]。correctOptionvalue =$ {o.correctOption}/>< br />
< / li>
< g:set var =countervalue =$ {++ counter}/>
< / g:每个>
< li>
< g:checkBox name =options [$ {counter}]。correctOption/>< br />
< / li>
< li class =add>
< / li>
< / ul>

< / div>

如果您不想点击链接,那么就是域类

 类MultipleChoiceQuestion {
字符串问题
静态约束= {
...
}
static hasMany = [options:MultipleChoiceOption]

class MultipleChoiceOption {
String answerOption
boolean correctOption
MultipleChoiceQuestion问题
static constraints = {
。 ..
}
}

}

  def create(){
[您可以使用多种选择性问题实例:multipleChoiceQuestionInstance = new MultipleChoiceQuestion(params)]
}

def save(){
println params
def multipleChoiceQuestionInstance = new MultipleChoiceQuestion(params) !multipleChoiceQuestionInstance.save(flush:true)){
render(view:create,model:[multipleChoiceQuestionInstance:multipleChoiceQuestionInstance])
return
}

flash.message = message(code:'default.created.message', args:[message(code:'multipleChoiceQuestion.label',default:'MultipleChoiceQuestion'),multipleChoiceQuestionInstance.id])
redirect(action:show,id:multipleChoiceQuestionInstance.id)
}

def update(){
def multipleChoiceQuestionInstance = MultipleChoiceQuestion.get(params.id)
if(!multipleChoiceQuestionInstance){
.... //删除房地产
返回
}

if(params.version){
//版本检查内容
}
}

multipleChoiceQuestionInstance.properties = params

if(!multipleChoiceQuestionInstance.save(flush:true)){
render(view:edit ,model:[multipleChoiceQuestionInstance:multipleChoiceQuestionInstance])
return
}

flash.message = message(code:'default.updated.message',args:[message(code: 'multipleChoiceQuestion.label',默认:'MultipleChoiceQuestion'),multipleChoiceQuestionInstance.id])
重定向(action:show,id:multipleChoiceQuestionInstance.id)
}

选项[计数器]为空。该空值作为参数传递给控制器​​操作,并用这些空值创建一个新行。



在调用

multipleCHoiceQuestionInstance.properties = params $之前,您应该删除任何空白选项b $ b



您可以使用如下所示:

  def emptyOptions = params.options.findAll {!it.answerOption} 
params.options.removeAll(emptyOptions)


I have been struggling with trying to create/save multiple instances at once in Grails, now I am almost there with the following code but when I hit save an empty row of options is created, can anyone help me with this please see these two questions to see what I want to achieve

How to save multiple object from one view using Grails

Grails one to many relationship view

<g:textField name="question" value="${multipleChoiceQuestionInstance?.question}"/><br/>
  <div class="fieldcontain ${hasErrors(bean: multipleChoiceQuestionInstance, field: 'options', 'error')} ">
    <label for="options">
      <g:message code="multipleChoiceQuestion.options.label" default="Options" />

    </label>

    <ul class="one-to-many">
      <g:set var="counter" value="${0}" />
      <g:each  status="i" in="${multipleChoiceQuestionInstance?.options?}" var="o">
        <li>
        <g:textField controller="multipleChoiceOption" name="options[${i}].answerOption" action="show" id="${o.id}" value="${o?.encodeAsHTML()}"/>
        <g:checkBox name="options[${i}].correctOption" value="${o.correctOption}"/><br/>
        </li>
        <g:set var="counter" value="${++counter}" />
      </g:each>
      <li>
      <g:textField name="options[${++counter}].answerOption" value=""/>
      <g:checkBox name="options[${counter}].correctOption" /><br/>
      </li>
      <li class="add">
      <g:link controller="multipleChoiceOption" action="create" params="['multipleChoiceQuestion.id': multipleChoiceQuestionInstance?.id]">${message(code: 'default.add.label', args: [message(code: 'multipleChoiceOption.label', default: 'MultipleChoiceOption')])}</g:link>
      </li>
    </ul>

  </div>

If you prefer not to click on the link here are the domain classes

Class MultipleChoiceQuestion {
    String question
    static constraints = {
        ...
    }
    static hasMany = [options:MultipleChoiceOption]

class MultipleChoiceOption{
    String answerOption
    boolean correctOption
    MultipleChoiceQuestion question
    static constraints = {
        ...
    }
}

   }

I am using automatically generated code by grails for the controller, it is as bellow

    def create() {
        [multipleChoiceQuestionInstance: new MultipleChoiceQuestion(params)]
    }

    def save() {
        println params
        def multipleChoiceQuestionInstance = new MultipleChoiceQuestion(params)
        if (!multipleChoiceQuestionInstance.save(flush: true)) {
            render(view: "create", model: [multipleChoiceQuestionInstance: multipleChoiceQuestionInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'multipleChoiceQuestion.label', default: 'MultipleChoiceQuestion'), multipleChoiceQuestionInstance.id])
        redirect(action: "show", id: multipleChoiceQuestionInstance.id)
    }

def update() {
        def multipleChoiceQuestionInstance = MultipleChoiceQuestion.get(params.id)
        if (!multipleChoiceQuestionInstance) {
            .... //deleted for real estate
            return
        }

        if (params.version) {
             //version checking stuff
            }
        }

        multipleChoiceQuestionInstance.properties = params

        if (!multipleChoiceQuestionInstance.save(flush: true)) {
            render(view: "edit", model: [multipleChoiceQuestionInstance: multipleChoiceQuestionInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'multipleChoiceQuestion.label', default: 'MultipleChoiceQuestion'), multipleChoiceQuestionInstance.id])
        redirect(action: "show", id: multipleChoiceQuestionInstance.id)
    }

解决方案

The reason that you are getting an empty row of options is because you are leaving a textfield with name option[counter] blank. This empty value is passed as a parameter to the controller action and it creates a new row with these blank values.

You should remove any blank options before calling

multipleChoiceQuestionInstance.properties = params

You can use something like this :

def emptyOptions = params.options.findAll{!it.answerOption}
params.options.removeAll(emptyOptions)

这篇关于Grails空入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆