views:

132

answers:

1

I am setting up a Rails site using map.resources and map.resource, and I am encountering a limitation (as far as I know at least).

Here is my current routing.

map.with_options :conditions => { :subdomain => true } do |sub|
  sub.root :controller => 'company'

  sub.resource :company do |company|
    company.resources :clients, :path_prefix => nil, :name_prefix => nil, :collection => { :sort => :post } do |clients|
      clients.resources :projects, :path_prefix => ":client/:project"
    end
  end

  sub.resource :user, :collection => { :logout => :get }
end

In a general sense this is my wanted outcome:
/client-name/action
Calls the Clients controller, and passes the client-name as some sort of hash that is the same.

/client-name/project-name/action
Calls the Projects controller, but passes the client-name as a hash that stays the same. It wants to pass that as an id.

I am hoping to keep my URL structure very basic, so:

  1. client-name = yahoo
  2. project-name = login-page

/client-name/ - This should use the Clients.show method.
/client-name/edit - This should use the Clients.edit method.
/client-name/project-name - This should use the Projects.show method.
/client-name/project-name/edit - This should use the Projects.edit method.

Although with my current routing it is being interpreted as such:
/client-name/projects - This is using the Projects.index method.
/clients/client-name - This is using the Clients.show method.


I really appreciate any help that is given.

+1  A: 

Resource mapping isn't that powerful. Besides, it adds the name of the resource (company, project, etc.) in the URI. There may still be a way to achieve what you're trying to do using resource mapping, but I think it's easier to use regular mapping:

map.connect ':client',      :controller => 'clients', :action => 'show'
map.connect ':client/edit', :controller => 'clients', :action => 'edit'

map.connect ':client/:project',      :controller => 'projects', :action => 'show'
map.connect ':client/:project/edit', :controller => 'projects', :action => 'edit'
Can Berk Güder
I'm pretty sure this is what you're looking for. If you're not using all the RESTful methods, I wouldn't use resources. Just go with standard routing.
Jon Smock
In Rails 3 my problem is fixed :)
Garrett