我是grails的新手,我正尝试用它编写一个简单的博客应用程序。我有一些资源,我正在尝试实现创建资源逻辑。
这是我所做的;
标签的URL映射;
"/tags"(resources: 'tags')
标签 Controller ;
class TagsController {
def index() {
[tags: Tag.list()]
}
def create() { }
def save() {
def tag = new Tag(params.tag)
if(tag.save()) {
flash.message = "Tag Created Successfully"
redirect(action: "index")
}
else {
flash.error = "Something went wrong, please check the form again!"
render(view: "create")
}
}
def show() {
render "this is the show action"
}
}
使用此配置,redirect(action:“index”)重定向到标签/索引路径。但是此路径不用于索引操作,而是用于显示操作。
我究竟做错了什么?
我已经尝试过这样的URL映射;
"/tags"(resources: 'tag')
在这种情况下,标记/和标记/索引路径触发索引 Action ,但是标记/ 6(6是数据库中现有标记的ID)不会触发显示 Action 。
最佳答案
"/tags"(resources: 'tags')
是正确的。
如果要从save
重定向到show
,则必须提供id
所需的show
。查看mapping grails如何将URL映射到 Action 。
要提供id
,您可以编写
redirect(action: "index", id: tag.id)
要么
redirect(action: "show", id: tag.id)
[更新:]
要重定向到
index
,您必须显式设置方法:重定向(操作:“索引”,方法:“获取”)
关于grails - grails redirect(action :'index')重定向以显示操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467652/