cybertextron
cybertextron

Reputation: 10961

passing id through the URL grails groovy

I'm writing a grails application, where I have region, district and streams. region contains district, so region_id is a foreign key for the district table. In other to query only rows associated with that region, I need to be able to pass the id to the URL. Here is my UrlMappings.groovy:

static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(controller:"/user")
        "500"(view:'/error')
    }

However, then I go to the link I created to forward to the district controller (default is list I see: localhost:8080/project/district/list, but it does not have the region_id, so I was expecting localhost:8080/project/district/list/region_id='1'? or localhost:8080/project/district/list/id='1'? Could someone please help me pointing out where is my mistake? Thanks

Upvotes: 1

Views: 3362

Answers (1)

Igor Artamonov
Igor Artamonov

Reputation: 35961

/$controller/$action?/$id? means that params maps will have property id with value from url. For url like /project/district/list/5 this map returns 5 (assert params.id == 5).

Also you can call your action by using following url: /project/district/list?region_id=5 and get 5 for params.region_id.

If you want to have different name, region_id instead of `id, and don't want to pass it as a query parameter, you can make your own mapping:

"/district/list/$region_id"(controller:"district", action: "list")

At this case this url is mapped strictly to controller district and action list and params.region_id will return 5 for /project/district/list/5

Upvotes: 3

Related Questions