Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

Grails empty entry into the database

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)
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...