grails <g:select>下拉列表无法显示正确的值</g:select>

时间:2014-07-24 12:50:09

标签: grails

这种下拉几乎按照我想要的方式运作。我无法显示答案对象的body属性或toString

<g:select name="questionId"
        from="${questionInstance.answers.id}"
        value="${questionInstance.correctAnswer.id}"
        noSelection="['':'Select a Module']" />

dropdown image

不幸的是,无论我尝试什么组合,我都无法在默认情况下选择correctAnswer,而不使用id属性中的from

<g:select name="questionId"
        from="${questionInstance.answers}"
        value="${questionInstance.correctAnswer}"
        noSelection="['':'Select a Module']" />

second image

我做错了什么?

问题

class Question {

    DateTime dateCreated
    DateTime lastUpdated

    String body
    Answer correctAnswer
    Integer ordinal

    static belongsTo = [lesson: Lesson]
    static hasMany = [answers: Answer]

    static constraints = {
        body blank: false
        correctAnswer nullable: true,
                validator: { Answer val, Question obj ->
                // Correct answer must have this as it's question
                val ? val.question == obj : true // TODO: Give this a proper error message
            }
        ordinal unique: 'lesson'
    }

    static mapping = {
        lesson lazy: false
        answers sort: 'ordinal'
    }
}

答案

class Answer {

    DateTime dateCreated
    DateTime lastUpdated

    String body
    Integer ordinal
    String reason

    static belongsTo = [question: Question]

    static constraints = {
        body blank: false
        ordinal unique: 'question'
        reason blank: false
    }

    static mapping = {
        question lazy: false
    }

    String toString() {
        "Answer: $body"
    }
}

2 个答案:

答案 0 :(得分:0)

您能提供域名问题和答案吗?

同时尝试添加属性optionKey =“id”optionValue =“name”

如果域名具有名称属性。

答案 1 :(得分:0)

我会说,这应该有效:

<g:select name="questionId"
    from="${questionInstance.answers}"
    value="${questionInstance.correctAnswer}"
    optionKey="id"
    optionValue="body"
    noSelection="['':'Select a Module']" />