Forum: Ruby on Rails Access the 'parent' resource from a nested one?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
84a473cc8e6842426532f3e4b9abe1f2?d=identicon&s=25 Carl H. (Guest)
on 2007-06-05 18:21
(Received via mailing list)
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
6e9db38e16957cc51cf9cee9de399249?d=identicon&s=25 Gabe da Silveira (Guest)
on 2007-06-05 18:39
(Received via mailing list)
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@gmail.com> wrote:
>     pics.resources :logos
>
> class Logo < FlexImage::Model
> http://localhost:3000/users/#{user_id}/logos/#{size}
>
> >
>


--
Gabe da Silveira
http://darwinweb.net
971708ecf1c0b3cada6cf98ea58611fd?d=identicon&s=25 Val (Guest)
on 2007-06-05 18:55
(Received via mailing list)
Carl,

You can find an approach you can use discussed here
http://revolutiononrails.blogspot.com/2007/05/dryi...

Regards,
Val
84a473cc8e6842426532f3e4b9abe1f2?d=identicon&s=25 Carl H. (Guest)
on 2007-06-06 12:14
(Received via mailing list)
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
This topic is locked and can not be replied to.