How do I make inherited_resource optionally nested like a singleton?

I am using inherited_resources for DRY for my controllers but cannot figure out how to properly manage a specific controller. In my model User has_one Person

. I want it to be optionally nested, behave like a single element when nested, and non-singleton when not nested. In other words, I want to be able to list all famous people (/ people), get person # 5 (/ person / 5), and get only 10 people (/ user / 10 / person). In route.rb:

resources :users
  resource :person
end
resources :people

      

... sets the routes as I expect:

         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

      

... so great. Now if in PeopleController I use:

belongs_to :user, :optional => true

      

... then non-nested / custom urls work, but urls nested / users /: user_id / person are not executed: undefined method 'people'

If instead in PeopleController I use:

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

      

... then the URLs of the nested / users /: user_id / person work, but the urls of the non-nested / people are unrelated to being treated as singleton even if they are not nested: undefined method 'person'

Summary: Is there a way to force inherited_resources to treat a resource as a singleton when accessed via a nested route, but as a non-singleton when accessed via a non-nested route?

+3


source to share


2 answers


In case anyone tries to do something like this, I just left inherited_resources. I find I am happier with less "magic" going on in my controllers.



+3


source


Apparently it is not supported: https://github.com/josevalim/inherited_resources/issues/169



0


source







All Articles