grails - grails redirect(action :'index')重定向以显示操作

标签 grails redirect grails-controller

我是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/

相关文章:

grails - 可以使用Grails的派生属性检索域对象吗?

spring-boot - 将 Grails 3 prod war 部署到 Tomcat9 的 MapperListener 错误是什么?

grails - 是否可以在运行时让grails应用在hibernate和mongodb之间进行选择

redirect - Magento 2 无法从管理 Controller 重定向到前端

grails - 在 UrlMappings 中使用 grailsApplication 注入(inject)

grails - Grails关系不保存

reactjs - 重定向为 "componentDidMount()": problem (React)

linux - 如何使用 shell 就地修改文件?

Grails - 交叉 Controller 代码,在每个请求上执行

grails - Grails 可以编写响应吗?