문제

내 컨트롤러를 DRY하기 위해 상속된_resources를 사용하고 있지만 특정 컨트롤러가 올바르게 작동하도록 만드는 방법을 알 수 없습니다.내 모델에서는 User has_one Person.선택적으로 중첩되고, 중첩되면 싱글톤으로 작동하고, 중첩되지 않으면 싱글톤이 아닌 것처럼 작동하기를 원합니다.즉, 알려진 모든 사람(/people)을 나열하고, 사람 #5(/person/5)를 얻고, 사용자 10의 유일한 사람(/user/10/person)을 얻을 수 있기를 원합니다.Routes.rb에는 다음이 포함됩니다:

resources :users
  resource :person
end
resources :people

...예상대로 경로를 설정합니다.

         user_person POST   /users/:user_id/person(.:format)                people#create
     new_user_person GET    /users/:user_id/person/new(.:format)            people#new
    edit_user_person GET    /users/:user_id/person/edit(.:format)           people#edit
                     GET    /users/:user_id/person(.:format)                people#show
                     PUT    /users/:user_id/person(.:format)                people#update
                     DELETE /users/:user_id/person(.:format)                people#destroy

              people GET    /people(.:format)                               people#index
                     POST   /people(.:format)                               people#create
          new_person GET    /people/new(.:format)                           people#new
         edit_person GET    /people/:id/edit(.:format)                      people#edit
              person GET    /people/:id(.:format)                           people#show
                     PUT    /people/:id(.:format)                           people#update
                     DELETE /people/:id(.:format)                           people#destroy

...그래서 큰.이제 PeopleController에서 다음을 사용합니다.

belongs_to :user, :optional => true

...중첩되지 않은 /people URL은 작동하지만 중첩된 /users/:user_id/person URL은 작동하지 않습니다. undefined method 'people' 대신 PeopleController에서 다음을 사용하는 경우:

belongs_to :user, :optional => true, :singleton => true

...중첩된 /users/:user_id/person URL은 작동하지만 중첩되지 않은 /people URL은 중첩되지 않은 경우에도 싱글톤으로 처리되기 때문에 작동하지 않습니다. undefined method 'person'

요약:상속된_resources가 중첩된 경로를 통해 액세스할 때는 리소스를 싱글톤으로 처리하지만, 중첩되지 않은 경로를 통해 액세스할 때는 비싱글톤으로 처리하도록 하는 방법이 있습니까?

도움이 되었습니까?

해결책

누구든지 비슷한 일을 하려고 할 경우를 대비해 나는 상속받은 리소스를 포기하게 되었습니다.컨트롤러에서 발생하는 "마법"이 줄어들수록 더 행복해집니다.

다른 팁

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top