我是完全陌生的。我有两个 Realm 类,一个问题与答案之间存在一对多的关系。

class Question {

    String question

    static hasMany = [answers: Answer]

    static constraints = {
        question blank: false
        answers minSize: 1
    }
}


class Answer {

    String answer

    static belongsTo = [question: Question]

    static constraints = {
        answer blank: false
    }
}

控制者

@Transactional(readOnly = true)
class QuestionController {

  static scaffold = Question

  static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

  def index() {
    List < Question > questionList = Question.list()[questionList: questionList]
  }

  def show() {
    [question: Question.get(params.id).question,
      answers: Question.get(params.id).answers
    ]
  }

  def create() {
    respond new Question(params)
  }

  @Transactional
  def save(Question question) {
    if (question == null) {
      transactionStatus.setRollbackOnly()
      notFound()
      return
    }

    if (question.hasErrors()) {
      transactionStatus.setRollbackOnly()
      respond question.errors, view: 'create'
      return
    }

    question.save flush: true

    request.withFormat {
      form multipartForm {
        flash.message = message(code: 'default.created.message', args: [message(code: 'question.label',
          default: 'Question'), question.id])
        redirect question
      }
      '*' {
        respond question, [status: CREATED]
      }
    }
  }

  def edit(Question question) {
    respond question
  }

  @Transactional
  def update(Question question) {
    if (question == null) {
      transactionStatus.setRollbackOnly()
      notFound()
      return
    }

    if (question.hasErrors()) {
      transactionStatus.setRollbackOnly()
      respond question.errors, view: 'edit'
      return
    }

    question.save flush: true

    request.withFormat {
      form multipartForm {
        flash.message = message(code: 'default.updated.message', args: [message(code: 'question.label',
          default: 'Question'), question.id])
        redirect question
      }
      '*' {
        respond question, [status: OK]
      }
    }
  }

  @Transactional
  def delete(Question question) {

    if (question == null) {
      transactionStatus.setRollbackOnly()
      notFound()
      return
    }

    question.delete flush: true

    request.withFormat {
      form multipartForm {
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'question.label',
          default: 'Question'), question.id])
        redirect action: "index", method: "GET"
      }
      '*' {
        render status: NO_CONTENT
      }
    }
  }

  protected void notFound() {
    request.withFormat {
      form multipartForm {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'question.label',
          default: 'Question'), params.id])
        redirect action: "index", method: "GET"
      }
      '*' {
        render status: NOT_FOUND
      }
    }
  }
}


现在,如果我要创建一个问题,答案将显示为/answer/create.gsp的链接,如下图所示

/question/create.gsp grails - 正确显示hasMany关系-LMLPHP

我正在寻找的答案是在问题/create.gsp中显示为文本字段

/question/create.gsp的形式:

 <g:form action="save">
   <fieldset class="form">
      <f:all bean="question"/>
   </fieldset>
   <fieldset class="buttons">
      <g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
   </fieldset>
 </g:form>


我认为必须为我的表单提供其他解决方案,但我不知道。

提前致谢!

最佳答案

作为仅带演示部件的简单示例,请导航至/ question / myShow

问题/myShow.gsp:

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main" />
    </head>
<body>
<g:form>
    <div class="fieldcontain required">
        <label for="question">Question</label>
        <g:textField name="question" value="${question.question}"/>
    </div>

    <div class="fieldcontain required">
        <label for="question">Answers</label>
        <g:each in="${question.answers}" var="ans" status="i">
            <g:textField name="ans${i}" value="${ans.answer}"/>
        </g:each>
    </div>
</g:form>
</body>
</html>

问题 Controller :
def myShow() {
    [question: Question.first()]
}

BootStrap.groovy
def q1 = new Question(question: 'what is 9 + 10?').save( failOnError: true )
new Answer( answer: '19', question: q1 ).save( failOnError: true )

关于grails - 正确显示hasMany关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44214729/

10-14 08:53