Groovy 帮助...关于 def 编辑和控制器

发布于 2024-11-01 21:48:34 字数 717 浏览 4 评论 0原文

def edit = {} 默认包含什么?你看,我正在关注一本书,但结果发现它使用的是旧版本,这就是为什么某些代码不起作用的原因。我有这段代码:

def edit= {
def user = User.get(params.id)
    if (session?.user?.id == null){
        flash.message = "You have to login first before editting your stuff."
        redirect(action:'login')
        return
    }else if(session?.user?.id != params.id) {
        flash.message = "You can only edit yourself."
        redirect(action:list)
        return
    }else{
        //What should I put here?
}

}

它已经可以运行了。如果用户在未登录的情况下单击编辑,则会被重定向到登录页面。否则,如果他登录了,那么他只能编辑自己。我应该在“else”子句中添加什么?它应该已经允许用户编辑他的东西,但我真的不知道如何实现我想要的。 :(

如果有人可以分享默认的 edit 代码片段,那就太好了。

我对所有这些都有点陌生,所以请放轻松。

What does def edit = {} contain by default? You see, I was following a book but it turns out to be using an older version that's why some of the code don't work. I have this piece of code:

def edit= {
def user = User.get(params.id)
    if (session?.user?.id == null){
        flash.message = "You have to login first before editting your stuff."
        redirect(action:'login')
        return
    }else if(session?.user?.id != params.id) {
        flash.message = "You can only edit yourself."
        redirect(action:list)
        return
    }else{
        //What should I put here?
}

}

It's already functional. If the user clicks on edit without logging in, then he's redirected to a login page. Otherwise, if he did login, then he's only allowed to edit himself. What should I put on the "else" clause? It should already should already allow the user to edit his stuff, but I don't really know how to implement what I want. :(

It would be great if someone could share the default edit snippet.

I'm a bit new to all these, so go easy on me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-11-08 21:48:35

如果您正在谈论 Grails,请备份您的 UserController 并尝试 grails generate-controller - 它将为您提供默认操作的完整文本。

我还建议您仔细阅读脚手架章节 - 这是一个很好的观点开始。

If you're talking about Grails, back up your UserController and try grails generate-controller - it will give you the complete text of default actions.

I also suggest that you look through scaffolding chapter - it's a great point to start.

长途伴 2024-11-08 21:48:35

默认编辑操作应该如下所示(伪代码,它取决于您创建代码的实际域类):

def edit = {
    redirect(action: "show", id: params.id)
    return true

    def <domain>Instance = <DomainClass>.get(params.id)
    if (!<domain>Instance) {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: '<DomainClass>.label', default: '<DomainClass>'), params.id])}"
        redirect(action: "list")
    }
    else {
        return [<domain>Instance: <domain>Instance]
    }
}

顺便说一句:大多数时候您不必通过在控制器中显式编程来进行安全检查代码,请为此目的查看 Grails Spring Security 插件

the default edit action should look like this (pseudo-code, it depends on the actual domain class you create the code upon):

def edit = {
    redirect(action: "show", id: params.id)
    return true

    def <domain>Instance = <DomainClass>.get(params.id)
    if (!<domain>Instance) {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: '<DomainClass>.label', default: '<DomainClass>'), params.id])}"
        redirect(action: "list")
    }
    else {
        return [<domain>Instance: <domain>Instance]
    }
}

btw: most of the time you don't have to do the security checks by programming these explicitly in the controller code, check out the Grails Spring Security Plugin for that purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文