2

I'm having trouble rewriting URL's in Grails:

I've got 2 controllers BlogController and ProjectsController each with a default def index = { } and matching view.

Now when I create the following links:

<g:link controller="blog">Blog</g:link>
<g:link controller="projects">Projects</g:link>

They get translated to http://localhost:8080/myapp/blog/index and http://localhost:8080/myapp/projects/index. But want them (and all other controllers default action) to be without the trailing /index.

Can anyone help me do this?

Jasper
  • 2,166
  • 4
  • 30
  • 50

2 Answers2

3

Try to specify action parameter in link tag as space.

<g:link controller="projects" action=" ">Projects</g:link>
amra
  • 16,125
  • 7
  • 50
  • 47
1

Try using a Named URL Mapping

Add this to your grails-app/conf/UrlMappings.groovy

    name blog: "/blog" {
            controller = "blog"
            action = "index"
    }
    name projects: "/projects" {
            controller = "projects"
            action = "index"
    }

and change your links to use the mapping parameter:

<g:link mapping="blog">Blog</g:link>
<g:link mapping="projects">Projects</g:link>
Colin Harrington
  • 4,459
  • 2
  • 27
  • 32
  • This works, but it doesn't really scale well. Every controller would need his own named mapping. – Jasper Aug 12 '10 at 19:46
  • The named mappings are actually excessive. If you just provide a mapping for /blog to the proper controller/action, the link tag will remap it correctly. So, "/blog"(controller: "blog", action: "index") is all that is needed. The link tag w/ controller="blog" action="index" will map back to "/blog" correctly. – tixxit Dec 16 '10 at 20:18