这是我的域:
class UserFeedBack
{
int rating1
int rating2
int rating3
int rating4
int rating5
}
这是我的控制器:
def addResponse(params)
{
UserFeedBack feedback_response = new UserFeedBack()
def answer =JSON.parse(params.answer)//this comes from ajax
for(int i=1;i<=5;i++)
{
if(feedback_response.rating+i!="")
{
feedback_response.rating+i=answer.rating+i.toInteger()
}
}
}
这是一个编译时间错误。
没有任何方法可以保存到数据库?
尝试这样的东西:
for(int i=1;i<=5;i++){
if(feedback_response['rating'+i]){
feedback_response['rating'+i] = (answer['rating'+i] as Integer)
feedback_response.save(flush:true)
}
}
如果要通过动态创建的名称访问对象的属性,则需要使用[]
。请记住"rating"
是字符串,需要在括号中。
您也可以在if
子句中省略!=""
,因为空字符串为falsy
值。
要坚持数据库的对象,您需要调用save
。