Grails将空条目导入数据库

Grails将空条目导入数据库,grails,gorm,gsp,Grails,Gorm,Gsp,我一直在努力尝试在Grails中一次创建/保存多个实例,现在我几乎可以使用下面的代码了,但是当我点击save时,创建了一个空的选项行,有人能帮我吗 请看这两个问题,看看我想要实现什么 我使用grails为控制器自动生成的代码,如下所示 def create() { [multipleChoiceQuestionInstance: new MultipleChoiceQuestion(params)] } def save() { pr

我一直在努力尝试在Grails中一次创建/保存多个实例,现在我几乎可以使用下面的代码了,但是当我点击save时,创建了一个空的选项行,有人能帮我吗 请看这两个问题,看看我想要实现什么

我使用grails为控制器自动生成的代码,如下所示

    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)
    }

您得到一个空选项行的原因是您将一个名为option[counter]的文本字段留空。此空值作为参数传递给控制器操作,并使用这些空值创建新行

在调用之前,应删除所有空白选项

multipleChoiceQuestionInstance.properties=params

您可以使用以下内容:

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

请在保存实体的地方张贴代码。您还没有真正告诉我们您的问题是什么,您目前看到的行为是什么,以及您希望看到的行为是什么。我不认为有太多的人会为了弄清楚这堵GSP代码的墙想要实现什么而去阅读另外两个问题。另外,您以前也被指导过这个项目,它包含了这个问题的解决方案。这个解决方案有什么你不喜欢的吗?@AnujArora请检查我的答案edit@don问题说明为“当我点击“保存选项的空行时”这意味着将在数据库中创建一个没有任何数据的空行。@Grrrrr:您能发布您在控制器中用于持久化实体的代码吗?
    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)
    }
def emptyOptions = params.options.findAll{!it.answerOption}
params.options.removeAll(emptyOptions)