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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gabe da Silveira
2007-Jun-05 16:37 UTC
Re: Access the ''parent'' resource from a nested one?
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. On 6/5/07, Carl H. <charroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > > >-- Gabe da Silveira http://darwinweb.net --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Carl, You can find an approach you can use discussed here http://revolutiononrails.blogspot.com/2007/05/drying-up-polymorphic-controllers.html Regards, Val On Jun 5, 12:21 pm, "Carl H." <charr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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} andhttp://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--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jun 5, 5:53 pm, Val <v.alekse...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Carl, > > You can find an approach you can use discussed herehttp://revolutiononrails.blogspot.com/2007/05/drying-up-polymorphic-c... > > Regards, > Val > > On Jun 5, 12:21 pm, "Carl H." <charr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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} andhttp://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--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---