senotrusov
senotrusov

Reputation: 809

Get the name of parent resource

The nested resources are defined in Rails:

resources :foo do
  resources :bar
end

resources :aaa do
  resources :bbb
end

I am trying to make application-wide layout with link to topmost resource, e.g. if user is somewhere in bar interface it will have handy link to /foos/.

And at the same time if the user is somethere in bbb the link will point to /aaas

So, the question is - in request to bar is any way to get the name of parent resource?

I know there is foo_id in params, but the problem is - I can't just get keys from params and hope that one of them with _id hint me to parent resource name.

Upvotes: 0

Views: 172

Answers (1)

nathanvda
nathanvda

Reputation: 50057

If you made sure that each nested model has a method called parent (or something similar), which returns the parent-object (obviously), then you could easily implement a link_to_parent in your application_helper.

Hope that helps.

Upvotes: 1

Related Questions