2017-05-27 7 views
0

私はGrailsにとって全く新しいです。回答に1対多の関係を持つ質問を持つ2つのドメインクラスがあります。は、grailsにhasMany関係が正しく表示されています。

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 
 
     } 
 
    } 
 
    } 
 
}

私が質問を作成したい場合は今、その答えは、/ /答えにリンクとして示されていますcreate.gsp、下の画像

私が探している何

/question/create.gspenter image description here

、答えは/question/create.gspの質問/ 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

デモの部分だけを持つ単純な例として、/質問/ 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()] 
} 

ブートストラップ。グルービー

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

私がn/question/myShowに移動すると、「プロパティを取得できません」という質問が 'nullオブジェクトに対して'返されます。私は何かが恋しいですか? – dtsch

+0

あなたのDBに質問がないと思いますか?上記で使用している「最初の」メソッドは、ID http://docs.grails.org/latest/ref/Domain%20Classes/first.htmlでDBから最初のレコードを取得します。 BootStrap.groovyの質問と回答をDBにシードする –

+0

はい、あなたはまったく正しいです。ありがとう! – dtsch

関連する問題