Paul Kaplan
Paul Kaplan

Reputation: 2885

Routes question: what if I want both a resource to be nested and on its own?

Objective:

Be able to nest a resource, like records inside of users so that I can access /users/1/records to see all of the first users records. But I would also like to see /records to see all of the records ( or the new ones, or something like that ).

Problem

So I know I am missing something somewhere because that cannot be the way to do it. I know I can have like a static page or some other route for that, but I don't think that is very rails-y.

I would have users, so a user would see their page with their records, but also be able to browse records, so I assumed I would need a more general route for that.

Question

What is the appropriate way to browse a nested resource?

PS I have looked at things like this question, which almost address the problem, but deals with a static landing page for non logged in users, this is not what I am looking for.

Upvotes: 4

Views: 113

Answers (1)

Jake Jones
Jake Jones

Reputation: 1170

Not sure how to handle this route later in controllers (never tried), but something like this should work:

resources :records

resources :users do
  resources :records
end

I think you'll need to do something in records's index controller to check if its called for some user or not.

UPD: Ok, checked it. Check for params[:user_id] in your index controller.

Upvotes: 2

Related Questions