rubyonrailsin

A Ruby and Rails talk

Tuesday, March 23, 2010


Re: [Rails] :has_many and :controller specified in routes.rb

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

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 <garysweaver@gmail.com>
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?

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive