grails - Problem with RichUI star rating -
i'm devolping rating system richui plugin grails. first had following code:
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) } }
which worked fine, problem code that, if user updates rating 1 artist create new rating instance instead of update rating value. came following code:
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) } }
but code, when rating value assigned new rating (params.rating) in "else" block, assigned random number around 50's (like 53). can not see problem. little appreciated. much.
i found out problem was. had convert input value of rating type double. so, following code working supposed to:
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) } }
Comments
Post a Comment