Subdomain for nested resources in Rails

My rails app is set to use subdomains as described in this RailsCast:

http://railscasts.com/episodes/221-subdomains-in-rails-3

However, right now, paths render like this:

http://organization.domain.com/organizations/1/edit

I have the controllers set up to choose the organization based on the subdomain already, so I'm wondering if there's a way to strip out the /organizations/:id portion of the paths, such that:

link_to edit_organization(@organization)

goes to http://organization.domain/edit, instead of http://organization.domain/organizations/:id/edit

Since there's going to be many nested resources within organizations (people, donations, etc), it's important that URL's don't end up incredibly long, and that the path generation method remains pretty straightforward.

Is there a way to do this?

Answers


You could use a route like:

resource :organization, :path => ""

The will cut down your url to 'http://organization.domain/:id/edit`.

Getting rid of the :id is tricky, and I dont think it can be done directly. What I would do is something like:

resource :organization, :path => "", :only => [] do
    match "index", :via => :get
    match "new", :via => :get
    match "show", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/}
    match "edit", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/}
    match "update", :via => :put, :constraints => {:subdomain => /[a-zA-Z]+/}
    match "create", :via => :post
end

Not very DRY, but I think it should work.


Need Your Help

App Crashed while getting Received Memory Warning while using PDF

ios iphone objective-c ipad memory-management

I am using vrf Reader 2.5.6 to support from iOS 4.3 for viewing pdf and with zoom options, If i continuously browse the pdf views after some time it is displaying recived memory warning message in

Memory not requested being allocated

c memory

I have written a simple memory allocator, basically creates a chain of allocated blocks, they are linked together using pointers stored in a metadata struct before the allocated area.