Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

后端 未结 3 1127
鱼传尺愫
鱼传尺愫 2021-02-04 15:00

I\'m trying to do a simple family reunion site with: \"posts\", \"families\", \"kids\", and \"pictures\". Ideally I\'d like the routes/relationships to be structured this way:<

3条回答
  •  长情又很酷
    2021-02-04 15:37

    Your code example that limited your nesting to 2 levels is quite near the answer. To avoid duplicate routes for fams->kids and kids, you can use the :only option with a blank array so that the 1st-level kids will not generate routes except in the context of kids->pictures, like so:

    resources :posts do
      resources :pictures
    end
    
    resources :fams do
      resources :pictures
      resources :kids
    end
    
    resources :kids, only: [] do # this will not generate kids routes
       resources :pictures
    end
    

    For the above code, you can use the following to construct your polymorphic edit url:

    polymorphic_url([fam, picture], action: :edit) # using Ruby 1.9 hash syntax
    polymorphic_url([kid, picture], action: :edit)
    

提交回复
热议问题