Is there a possibility in an action to get the parent controller for
which the nested resource is called? for instance if I have the
following in my routes.rb:
map.resources :user do |users|
users.resources :logos
end
map.resources :pic do |pics|
pics.resources :logos
end
How easy would it be to get the parent? for instance I could call the
following 2 URLs:
http://localhost:3000/pics/#{pic_id}/logos/#{logo_id} and
http://localhost:3000/users/#{user_id}/logos/#{logo_id}
Can I know which parent I am currently in?
Further explanations follows:
The logo class is a polymorphic class defined as follow:
class Pic < ActiveRecord::Base
has_one :logo, :as => :logoable
end
class User < ActiveRecord::Base
has_one :logo, :as => :logoable
end
class Logo < FlexImage::Model
belongs_to :logoable, :polymorphic => true
end
Now in my logo controller, I want to show the correct logo for the
correct parent controller. The URL would look something like:
http://localhost:3000/pics/#{pic_id}/logos/#{size}
Or
http://localhost:3000/users/#{user_id}/logos/#{size}
in my show action in the logo controller I have something like:
logo = Logo.find(:first, :conditions => {:logoable_type
=> …, :logoable_id => …})
logoable_type is either User or Pic. However, I can not see how to get
that value easily. I could play around with the param hash as it will
return either pic_id or user_id. Is there any simpler way to know
which parent the nested ressource is called from?
The above to follow DRY. Is there a way to map back from the action to
the parent controller?
I probably need a bit of REST vocabulary but hopefully it is clear
enough.
Regards,
Carl