Search code examples
model-view-controllergrailsurl-mapping

Grails UrlMapping - cannot set params.id for controller


I'm trying to create a UrlMapping to map some static URL to a controller action, but I can't seem to set params.id. I'm working through Grails in Action, so I'm not sure what's wrong here.

Code:

class UrlMappings
...
static mappings={
    "/timeline/chuck_norris"(controller:'post',action:'timeline',id:'chuck_norris')
...
}

In PostController

def timeline{
   def user = User.findByUserId(params.id)
   [user:user]
}

Error: No signature of method: ...findByUserId() is applicable for argument types: () values: []

What's wrong with the above code? I'm using grails 1.2.2.


Solution

  • You need to use a closure:

    "/timeline/chuck_norris"{
                controller = 'post'
                action = 'timeline'
               id = 'chuck_norris"
            }
    

    or like this :

    "/timeline/$id"{
                   controller = 'post'
                    action = 'timeline'
    }