开发者

Rails 3 Route Wildcards

I am currently setting up routes in a Rails 3 application, and would appreciate it if someone could point me in the right direction.

Essentially, I am setting up some routes to provide data for some chained dropdowns, and would like to be able to have routes like:

/vehicles/t开发者_如何学JAVArims/01299999
/vehicles/trims/02299999

Where 01299999 and 02299999 are ids for two different parameters (in this case lets say 01299999 loads :vehicle_make into the params and 02299999 is :fuel_type in the params). I'm aware that I can ensure they get routed to the correct controller action and load the correct params by using constraints.

What makes this complicated, however, is that I would like to be able to have more than one option specified in the url, in any order, e.g.:

/vehicles/trims/01299999/02299999

or

/vehicles/trims/02299999/01299999

and these would load the correct params based on the structure of the id number (so if it starts with '012' it should always be :vehicle_make and if it starts '022' it should always be :fuel_type. How can I do this?

As a side note, it would be great if anyone could indicate how to constrain the id column for my models, so that when a vehicle make is created, its id should always start 012 and so on.

Thanks for any assistance!


A route wildcard catches any number of URL elements separated by / and looks like this:

match '/vehicles/trims/*trims' => 'vehicles#trims'
# change 'vehicles#trims' to match your controller and action names

You say:

As a side note, it would be great if anyone could indicate how to constrain the id column for my models, so that when a vehicle make is created, its id should always start 012 and so on.

As a rule, primary keys should not be meaningful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜