我有域名:评论,Secuser和评级。
我希望每个评论都可以通过SecUser +1或-1评级一次。在相关评论(对于域名讨论)的视图中,我希望有一个按钮来对评论进行投票或向下投票并刷新视图。 我到目前为止的代码: 评论观点:
<tbody>
<g:each in="${comments}" var="comm">
<tr>
<td>${comm.comment}</td>
<td>${comm.commentBy}</td>
<td><g:formatDate format="dd.MM.yyyy HH:mm" date="${comm.createDate}"/> </td>
<td><button type="button" class="btn btn-default btn-lg" onclick="${remoteFunction(action: 'ratePositiveComment', update: 'content', params:[commentID:"${comm.id}"])}"><span class="glyphicon glyphicon-thumbs-up" aria-hidden="false" style="float:right">${comm.numberPositiveRatings}</span></button>
<button type="button" class="btn btn-default btn-lg" onclick="${remoteFunction(action: 'rateNegativeComment', update: 'content', params:[commentID:"${comm.id}"])}"><span class="glyphicon glyphicon-thumbs-down" aria-hidden="false" onclick="addRating(-1, ${comm.id})" style="float:right">${comm.numberNegativeRatings}</span></button>
</td>
</tr>
</g:each>
</tbody>
评论控制员:
class Comments {
static belongsTo = Discussion
Discussion discussion
SecUser commentBy
String comment
Date createDate = new Date()
static hasMany = [commRatings : Rating]
public long getNumberPositiveRatings() {
return Rating.countByCommentRatedAndRate(this, 1);
}
public long getNumberNegativeRatings() {
return Rating.countByCommentRatedAndRate(this, -1);
}
List raters() {
return commRatings.collect{it.ratingUser}
}
List addToPosRatingUser(SecUser user) {
Rating.positiveRating(user, this)
//return raters()
}
List addToNegRatingUser(SecUser user) {
Rating.negativeRating(user, this)
// return raters()
}
评分域名:
class Rating {
static belongsTo = Comments
int rate
SecUser ratingUser
Comments commentRated
static Rating positiveRating(ratingUser, commentRated) {
def m = Rating.findByRatingUserAndCommentRated(ratingUser, commentRated)
if (!m) {
m = new Rating()
ratingUser?.addToRating(m)
commentRated?.addToRating(m)
m.rate = 1;
m.save()
}
return m
}
static Rating negativeRating(ratingUser, commentRated) {
def m = Rating.findByRatingUserAndCommentRated(ratingUser, commentRated)
if (!m) {
m = new Rating()
ratingUser?.addToRating(m)
commentRated?.addToRating(m)
m.rate = -1;
m.save()
}
return m
}
}
在域名SecUser(Spring安全插件)中,我添加了:
class SecUser {
transient springSecurityService
String username
String password
String userEmail
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static hasMany = [ratings:Rating]
List ratedComments() {
return ratings.collect{it.commentRated}
}
List addPosCommentRating(Comments comm) {
Rating.positiveRating(this, comm)
return ratedComments()
}
List addNegCommentRating(Comments comm) {
Rating.negativeRating(this, comm)
return ratedComments()
}
CommentsController是scaffolded,唯一增加的功能是:
def ratePositiveComment() {
def rater = SecUser.findById(springSecurityService.currentUser.id);
if(rater!=null) {
Comments comm = Comments.get(params.commentID);
comm.addToPosRatingUser(rater);
comm.save();
}
}
def rateNegativeComment() {
def rater = SecUser.findById(springSecurityService.currentUser.id);
if(rater!=null) {
Comments comm = Comments.get(params.commentID);
comm.addToNegRatingUser(rater);
comm.save();
}
}
我得到的错误代码是:
No signature of method: ForumProject.SecUser.addToRating() is applicable for argument types: (ForumProject.Rating) values: [ForumProject.Rating : (unsaved)]
Possible solutions: addToRatings(java.lang.Object). Stacktrace follows:
Message: No signature of method: ForumProject.SecUser.addToRating() is applicable for argument types: (ForumProject.Rating) values: [ForumProject.Rating : (unsaved)]
Possible solutions: addToRatings(java.lang.Object)
答案 0 :(得分:0)
addTo
(和removeFrom
)方法源自hasMany
地图中的名称。如果您已宣布
static hasMany = [rating:Rating]
然后addTo
方法为addToRating
,您的代码就是正确的。现在,您只需将呼叫更改为addToRatings
。
P.S。这很糟糕:
def rater = SecUser.findById(springSecurityService.currentUser.id);
springSecurityService.currentUser
已经是你想要的了,SecUser
用于在登录时创建Authentication
,并且它是通过查询从数据库中检索的,而不是从一些缓存。但是你在获得id
之后丢弃它,这样你就可以使用低效的动态查找器再次加载它。始终使用get
代替findById
。那么你的代码就应该是
def rater = springSecurityService.currentUser