I have a problem where I need values passed in from a GET request and I don’t know how to set up the routing definition.
My Category object has a type(string),a color(string) and many products. I want to create a simple web service that lets the caller get all of a Category’s products by passing in the Category’s type and color:
http://www.myapp.com/getProducts?catType=toy&color=red
or ?
http://www.myapp.com/categories/getProducts?catType=toy&color=red
How do I define the correct routing for this situation? Are there better ways to do this in a Restful manner… because I know that Rails is Restful, so if there is a way to do it “correctly” then that would be even better.
Thanks
Your first example:
In controller you will have catType and color in params hash:
Is there better way? Probably yes, but it depends on your needs. If you will always have catType and color parameters, than you can add route like this:
You will have access to those parameters with params hash like in previous example. And your urls will look like this:
If your parameters may change, you can use route globbing:
Then it will catch all request that has
www.my.app.com/getProduct/...at the begining. But you will have more work in controller. You will have access toquerywith this:and for
www.myapp.com/getProduct/color/red/catType/toyit will give:So you have to parse it manualy.