Search code examples
grailshttp-redirectgrails-controller

grails redirect(action:'index') redirects to show action


I'm newbie on grails and I'm trying to write a simple blog application with it. I have some resources and i'm trying to implement a create resource logic. Here is what i have done;

URL mapping for tags;

"/tags"(resources: 'tags')

TagsController;

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"
  }

}

With this configurations redirect(action: "index") redirects to tags/index path. But this path is not for index action it is for show action.

What am I doing wrong?

I've tried URL mapping like this;

"/tags"(resources: 'tag')

In this case tags/ and tags/index paths triggers to index action but tags/6 (6 is an id for existing tag on the database) does not triggers show action.


Solution

  • "/tags"(resources: 'tags') is correct.

    If you want to redirect from save to show you have to provide the id required by show. See mapping how grails maps the urls to actions.

    To provide the id you can write

    redirect(action: "index", id: tag.id)
    

    or

    redirect(action: "show", id: tag.id)
    

    [Update:]

    To redirect to index you have to explicitly set the method:

    redirect(action: "index", method:"GET")