grails更新收藏夹onclick

时间:2015-05-21 10:04:05

标签: grails

我正在寻找设置myFavorite表的方法,其中点击列表项我希望将其保存到myFavorite。

我在这里

class user {  
    static hasMany [favorite : Favorite]
}

class Favorite {
    //(no fields) just the two static's below
    static belongsTo [user :user]
    static hasMany [music : Music]
}

static constraints = {
    id bindable: true 
    //id generator:'assigned'
    user unique: true
    music nullable: true
}

目的是为每个人保留一个favorite.id并在同一个id下添加音乐。

这是我的控制器

def save() {
    def authenticatedUser = User.findByUsername(springSecurityService.principal.username)
    def principal = springSecurityService.principal

    int userid = principal.id // id of the person logged in
    def favorits = new Favorite(params) // form data
    def check = Favorite.findByUsers(authenticatedUser) // check if myFavorite exist for user
    def ids = check.id // id of the myFavorite
    def tracks = check.tracks.id + favorits.tracks.id

    def test2 = new Favorite(id:ids, 'users': userid, 'tracks': tracks)

    if (check == null){// if user don't have Favorite create one
        authenticatedUser.addToFavorite(favorits)
        authenticatedUser.save flush: true

    }

    //render([test2] as JSON)
    //test2.save flush: true
    update(test2)
}

" test2.save flush:true"但它会创建新的Favorite.ID instand来更新我传递的id。

更新(test2)还会创建一个新的id,用于更新我正在传递的id。

1 个答案:

答案 0 :(得分:0)

不要这样做:

#site-wrapper

您在这里创建了一个全新的$('body> div').find('script:not(script[type="IN/Share"])').remove().end().wrapAll('<div id="site-wrapper"></div>'); 实例。如果要更新现有的def test2 = new Favorite(id:ids, 'users': userid, 'tracks': tracks) ,则需要从数据库中检索现有实例,然后对其进行更改。尝试这样的事情:

Favourite