这种下拉几乎按照我想要的方式运作。我无法显示答案对象的body
属性或toString
。
<g:select name="questionId"
from="${questionInstance.answers.id}"
value="${questionInstance.correctAnswer.id}"
noSelection="['':'Select a Module']" />
不幸的是,无论我尝试什么组合,我都无法在默认情况下选择correctAnswer
,而不使用id
属性中的from
。
<g:select name="questionId"
from="${questionInstance.answers}"
value="${questionInstance.correctAnswer}"
noSelection="['':'Select a Module']" />
我做错了什么?
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"
}
}
答案 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']" />