Access the 'parent' resource from a nested one?

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

I don't see what would be simpler than using pic_id or user_id (maybe there is something?). From those you can generate any kind of setup you want, just put in a before_filter so it's available to all actions. Or do something like:

private def pic?   !params[:pic_id].blank? end

Not sure if there's anything built-in, but I don't see the need for it.

Carl,

You can find an approach you can use discussed here

Regards, Val

Val,

Thanks for the post, that looks to be exactly what I am looking for. I will leave any comments if any on the article.

Regards, Carl