Вопрос

I am using Rails 3.2 and am attempting to make a scoped nested resource (not sure if this is the right terminology). I have several groups and I want each group to have its own set of pages, like so:

/groups/1/pages/1
/groups/1/pages/2
/groups/1/pages/3
/groups/2/pages/1
/groups/2/pages/2
/groups/3/pages/1
/groups/3/pages/2
/groups/4/pages/1
etc.

First I created a groups controller and a pages controller and nested them like

resources :groups do
  resources :pages
end

The problem I was having is that I could see group 1's pages from groups 2, 3, and 4 by changing the :group_id in the URL. Also, the page ids were unique across all groups, not just limited to a single group.

Next I tried making a nested controller by doing rails g controller groups/pages index show new create edit update destroy but I was also unable to get this to work. I do not know enough Rails terminology to figure out what to Google.

Это было полезно?

Решение

What I think you are trying to accomplish is associations. And instead of generating controllers, you should probably generate scaffolds (which includes a model, controller, and default views). At the very least, you will need group and page models.

Then add has_many :pages in your group model. And add belongs_to :group in your page model. Then you need to add a group_id column to your page model/database.

However, if I'm wrong and all you are trying to do is paginate groups, then use the will_paginate gem.

I would also suggest you go through Mike Hartl's tutorial. It will give you a great background and you will learn rails terminology and basic techniques.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top