Routing - newbie question about shorting the url

Right now I have my url showing as the following:

I would like it to be:
(or even http://localhost/italian/special-pasta-sauce)

my category model has_many :recipes and my recipes model
belongs_to :category (for the purposes of my test project a recipe
can only belong to one category)

Is this possible in RoR? Any pointers?

This is possible but you will basically be setting up a rule like this

“whenever we get a url like /foo/bar then we want the category ‘foo’ and
the recipe ‘bar’”

That might clash with the existing url schema on your site. If you want
to go for it though then set up your routes like this:

map.category_recipe ‘:category_name/:recipe_name’, :controller =>
‘recipes’, :action => ‘show’, :conditions => {:method => :get}

Make sure you put it down the bottom, above the

map.connect ‘:controller/:action/:id’


Now the url http://localhost/italian/special-pasta-sauce

should call the recipes controller, with params like {:recipe_name =>
“special-pasta-sauce”, :category_name => “italian”}

And in your views you can say eg
category_recipe_path(category, recipe)

The above assumes that you’ve overriden to_param in Category and Recipe
to use the name of the object instead of it’s id.

If you’re using slus or something instead of name then you can
substitute appropriately.