Groovy 帮助...关于 def 编辑和控制器
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在谈论 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.
默认编辑操作应该如下所示(伪代码,它取决于您创建代码的实际域类):
顺便说一句:大多数时候您不必通过在控制器中显式编程来进行安全检查代码,请为此目的查看 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):
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.