gpt4 book ai didi

grails - RichUI星级评定存在问题

转载 作者:行者123 更新时间:2023-12-02 14:37:42 25 4
gpt4 key购买 nike

我正在为Grails开发带有RichUI插件的评分系统。首先,我有以下代码:

class RatingController {

def springSecurityService

static scaffold = true

def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def currentUser = currentUser()

currentUser.addToRatings(new Rating(artist:artist, rating:rating)).save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])

}

private currentUser(){
return User.get(springSecurityService.principal.id)
}
}

效果很好,但是此代码的问题在于,如果用户更新一位艺术家的评级,它将始终创建一个新的Rating实例,而不仅仅是更新评级值。
所以我想出了以下代码:
class RatingController {

def springSecurityService

static scaffold = true

def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def currentUser = currentUser()

if(! currentUser.ratings.artist.contains(artist)){
currentUser.addToRatings(new Rating(artist:artist, rating:rating)).save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}

else{
currentUser.ratings.find{it.artist==artist}.rating = rating
currentUser.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}

private currentUser(){
return User.get(springSecurityService.principal.id)
}
}

但是,使用此代码,当在“else”块中将等级值分配给新等级(params.rating)时,会将其分配给大约50的某个随机数(如53)。我看不出问题出在哪里。
一点帮助将不胜感激。
非常感谢。

最佳答案

我才发现问题出在哪里。我必须将等级的输入值转换为double类型。因此,以下代码可以正常运行:

class RatingController {

def springSecurityService

static scaffold = true

def rate = {
def rating = params.rating.toDouble()
def artist = Artist.get( params.id )
def currentUser = currentUser()

if(! currentUser.ratings.artist.contains(artist)){
currentUser.addToRatings(new Rating(artist:artist, rating:rating)).save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}

else{
currentUser.ratings.find{it.artist==artist}.rating = rating
currentUser.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}

private currentUser(){
return User.get(springSecurityService.principal.id)
}
}

关于grails - RichUI星级评定存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com