Forum: Ruby on Rails Routing through parent

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ryan M. (Guest)
on 2009-01-22 13:39

At the moment I have the following route in my routes.rb file

map.resources :categories do |categories|
    categories.resources :products

Simple relationship...
product belongs_to a category
a category has_many products

is it possible to create a route where I could have something like:

map.product '/:category_name/:product_name'

I can't seem to get both of the controllers working together to create
such route.

Thanks in advance.

Andrew T. (Guest)
on 2009-01-22 14:02
(Received via mailing list)
On Thu, Jan 22, 2009 at 1:39 PM, Ryan M. <
removed_email_address@domain.invalid> wrote:

> product belongs_to a category
> McKenzie
> --
> Posted via
> >
You can do that with map.connect ':category_name/:product_name',
=> <controller_name>, :action => <action_name>

You'll have to think about how good an idea it is to use only category
product names and not ids

Think about using to_param on your models which can then display both id
class Category < ActiveRecord::Base
  def to_param
    "#{id}-#{name.gsub(/[^\w\d]+/, '-')}"

Then you don't have to mess with your routes and your urls will be
/1-category/2-product and your controllers will still get the id in

Andrew T.

"I have never let my schooling interfere with my education" - Mark Twain
This topic is locked and can not be replied to.