Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:37+00:00 2026-05-28T01:32:37+00:00

I have been struggling with trying to create/save multiple instances at once in Grails,

  • 0

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)
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:32:38+00:00Added an answer on May 28, 2026 at 1:32 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been struggling for days now, trying to simply create a new
I have been struggling with this question for awhile now, and I haven't reached
I have been struggling for the last few hours trying to get this search
I have been struggling with trying to send an email with an attachment using
I have been trying to create a new raster object that contains only a
I have been struggling with this for some time now scanning the net for
I've been struggling with this for a couple days now. I'm trying to make
I'm trying to create an assert method in Javascript. I've been struggling with arguments.callee.caller
Been struggling with this one I have the following DOM Structure: <ol id=add_images> <li>
I have been struggling for days trying to find the proper way to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.