我对Grails完全不熟悉。我有两个域类,其中一个问题与答案有一对多的关系。
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的链接,如下图所示
我正在寻找的是,答案在问题/ 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>
我认为我的表单必须有另一种解决方案,我不知道。
提前致谢!
答案 0 :(得分:0)
仅作为演示部分的简单示例,导航到/ 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>
问题控制员:
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 )