RESTfully Nesting Resource Routes with Single Identifiers

前端 未结 4 1912
情话喂你
情话喂你 2021-02-06 04:47

In my Rails app I have a fairly standard has_many relationship between two entities. A Foo has zero or more Bars; a Bar belongs to exactly

4条回答
  •  佛祖请我去吃肉
    2021-02-06 05:27

    Rails' shallow nesting gives us the best of both worlds: it lets us have Foo-specific routes where it makes sense (create, new, index) and Bar-only routes otherwise (show, edit, update, destroy).

    Unfortunately, the new routing DSL in Rails 3 (which I'm using) does not support shallow routing yet (as of Beta 3). It appears to be in the works but it's not functional for today.

    However, you can fake it by mapping the resource twice, once nested and once shallow:

    resources :foo do
      resources :bar, :only => [:index, :new, :create]    
    end
    resources :bar, :only => [:show, :edit, :update, :destroy]
    

    This will tide me over until the :shallow option is up and running again.

提交回复
热议问题