7

This is my domain model, a survey has many questions, and each question has many repsonses :

class Survey {

    String name
    String customerName
    static hasMany = [questions: SurveyQuestion]

    static constraints = {
    }
}

class SurveyQuestion {

    String question

    static hasMany = [responses : SurveyQuestionResponse]
    static belongsTo = [survey: Survey]

    static constraints = {
    }
}

class SurveyQuestionResponse {

    String description
    static belongsTo = [question: SurveyQuestion]

    static constraints = {
    }
}

In my controller, I have a method that takes in the ID for a Survey, looks it up, then builds a question from another request parameter, tries to add the question to the survey and save it:

def addQuestion =
    {
        def question = new SurveyQuestion(question:params.question)
        def theSurvey = Survey.get(params.id)

        theSurvey.addToQuestions(question) //fails on this line
        theSurvey.save(flush:true)

        redirect(action: showSurvey, params:[id:theSurvey.id])
    }

However, it fails and returns this :

No signature of method: roosearch.Survey.addToQuestions() is applicable for argument types: (roosearch.SurveyQuestion) values: [roosearch.SurveyQuestion : null] Possible solutions: addToQuestions(java.lang.Object), getQuestions()

I'm not quite understanding what I'm doing wrong here, I've tried various alternative ways to create the question, even instantiating one manually with a literal string, but it always gives the same error.

Can anyone please advise me?

Thanks

4

3 回答 3

1

问题是您没有保存“问题”,所以它还没有在数据库中。尝试先保存“问题”,然后将其添加到调查中。像这样的东西:

def addQuestion =
    {
        def question = new SurveyQuestion(question:params.question).save()
        def theSurvey = Survey.get(params.id)

        theSurvey.addToQuestions(question)
        theSurvey.save(flush:true)

        redirect(action: showSurvey, params:[id:theSurvey.id])
    }
于 2015-05-06T13:14:00.517 回答
0

您可以尝试断言 SurveyQuestion 是否实际上是使用输入参数创建的?例如

assert question 

就在这条线之后

def question = new SurveyQuestion(question:params.question)

并按照#alcoholiday 的建议尝试一些日志记录。或者一个简单的

println params

可以让你快速浏览一下

于 2014-05-20T17:21:09.580 回答
0

(我没有足够的评论点,所以我会“回答”)。

首先,它看起来确实“OK”。

我已经学会了以错误信息的面值看待错误信息。由于某种原因,它认为“问题”是空的。我猜你可以插入一些日志,然后发现它不是。

此时,我会先尝试保存问题,查看它是否正确保存并获得分配和 id,然后调用 addToQuestions。

于 2013-08-03T01:21:22.943 回答