On Thu, Jan 22, 2009 at 1:39 PM, Ryan Mckenzie <
rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>
> Hello,
>
> At the moment I have the following route in my routes.rb file
>
> map.resources :categories do |categories|
> categories.resources :products
> end
>
> 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.
>
> McKenzie
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
You can do that with map.connect
'':category_name/:product_name'', :controller
=> <controller_name>, :action => <action_name>
You''ll have to think about how good an idea it is to use only category
and
product names and not ids
Think about using to_param on your models which can then display both id and
name
class Category < ActiveRecord::Base
def to_param
"#{id}-#{name.gsub(/[^\w\d]+/, ''-'')}"
end
end
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
params[:id]
--
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake
"I have never let my schooling interfere with my education" - Mark
Twain
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---