:has_many and :controller specified in routes.rb

Have an app using older version of Rails (2.3.2) that I need some
routing assistance with if anyone has a minute.

The app was originally designed to be purely html view, and an xml and
json api was hacked on. To keep it clean, we are now moving the first
version of the api (v1) under its own directory (v1) under app/
controllers but still responding to the old paths.

One of the parts of the new routes.rb specifies:
map.resources :buckets, :has_many => :apples, :controller => ‘v1/
buckets’

However, when I do rake routes, this produces:

buckets GET /buckets(.:format) {:controller=>“v1/
buckets”, :action=>“index”}
POST /buckets(.:format) {:controller=>“v1/
buckets”, :action=>“create”}
new_bucket GET /buckets/new(.:format) {:controller=>“v1/
buckets”, :action=>“new”}
edit_bucket GET /buckets/:id/edit(.:format) {:controller=>“v1/
buckets”, :action=>“edit”}
bucket GET /buckets/:id(.:format) {:controller=>“v1/
buckets”, :action=>“show”}
PUT /buckets/:id(.:format) {:controller=>“v1/
buckets”, :action=>“update”}
DELETE /buckets/:id(.:format) {:controller=>“v1/
buckets”, :action=>“destroy”}
bucket_apples GET /buckets/:bucket_id/apples(.:format)
{:controller=>“apples”, :action=>“index”}
POST /buckets/:bucket_id/apples(.:format)
{:controller=>“apples”, :action=>“create”}
new_bucket_apple GET /buckets/:bucket_id/apples/new(.:format)
{:controller=>“apples”, :action=>“new”}
edit_bucket_apple GET /buckets/:bucket_id/apples/:id/edit(.:format)
{:controller=>“apples”, :action=>“edit”}
bucket_apple GET /buckets/:bucket_id/apples/:id(.:format)
{:controller=>“apples”, :action=>“show”}
PUT /buckets/:bucket_id/apples/:id(.:format)
{:controller=>“apples”, :action=>“update”}
DELETE /buckets/:bucket_id/apples/:id(.:format)
{:controller=>“apples”, :action=>“destroy”}

(sorry for the bad formatting)

Notice that the routes for the actions specific to the model
referenced by :has_many ignores the specification of the controller
(they go to :controller=>“apples” instead of :controller=>“v1/
apples”).

Sorry for the n00b routing question. I didn’t find such an example of
how to set the controller of the :has_many referenced model yet, but
maybe it is an option somewhere, or I could setup another route for
that?

Maybe you could use:

map.resources :buckets, :controller => ‘v1/buckets’ do |buckets|
buckets.resources :apples, :controller => ‘v1/apples’
end

I know this is a bit more code than just using :has_many, but I don’t
think
:has_many lets you specify a different controller.

/Lasse

2010/3/23 gsw [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs