Rails Namespace vs. Nested Resource

前端 未结 2 2092
一向
一向 2021-01-30 15:21

Let\'s say my app has two models, Foo and Bar.

Foo optionally belongs_to Bar.

Right now I can look at a single Foo, or search for a particular Foo, and the FoosC

2条回答
  •  感动是毒
    2021-01-30 15:34

    I think what you're trying to achieve is:

    1. Bar has many Foos
    2. View Foos belonging to Bar
    3. View all Foos regardless of parent.

    You can achieve that with: routes.rb:

    resources :foos
    resources :bars do
      resources :foos, :controller => 'bars/foos'
    end
    

    The route helpers you end up with are:

    • bars_path
    • foos_path
    • bars_foos_path
    • etc, etc, 'rake routes' for the rest =)

    In essence, you end up with:

    • app/BarsController (rails g controller bars)
    • app/FoosController (rails g controller foos)
    • app/bars/FoosController (rails g controller bars/foos)

    In FoosController, you would access foos as usual with:

    @foos = Foos.all
    

    and in bars/FoosController, you would access bar's foos with:

    @foos = @bar.foos
    

    where bar can be pre-retrieved in the bars/foos controller with:

    before_filter :get_client
    
    private
    def get_client
      @bar = Bar.find(params[:bar_id])
    end
    

    Hope this helps. =)

    Edit: As for namespaced routes, I've personally used them when I some of my resources retrieved from a sub-path. For example, if I have an admin section of my site, then I might have the following:

    routes.rb:

    namespace :admin do
      resources :foos
    end
    

    and I create my controller with:

    rails g controller admin/foos
    

    This sets up my foos resource, such that I can access it at "my site url"/admin/foos, and also get helpers such as admin_foos_path.

提交回复
热议问题