0

I'm having my routes clash which I anticipated but i cant figure out how to shallow nest a resource to get my desired result. (or at least i think shallow nesting is what needs to be done)

I'm having these 2 urls clash

category_item show page

guides/:guide_id/categories/:category_id/:id

clash with category edit, new etc.. pages

/guides/:guide_id/categories/:id/edit

I'm using the friendly id gem so it thinks 'edit' is :id

I want to change the category_item url structure to

guides/:guide_id/:category_id/:id 

(minus the /categories for category_item)

This means the pages that are displayed to most people and picked up by search bots have shorter prettier urls and it stops the urls from clashing.

I just cant quite get it to happen.

here is the route

resources :guides do
    resources :categories,     only: [:new, :create, :edit, :update] do
       resources :category_items, path: "", shallow: true,  only: [:update, :new, :create, :edit, :show]
       resources :category_item_keys, path: "keys", only: [:update, :new, :create] do
          get :edit, on: :collection #-> url.com/guides/:guide_id/:category_id/keys/edit
       end
    end
end

I only want the /categories to be removed for category_items if possible.

Rob
  • 1,835
  • 2
  • 25
  • 53

2 Answers2

0

You can just make a separate route for it like this:

resources :guides do
  resources :categories,     only: [:new, :create, :edit, :update] do
    resources :category_items, path: "", only: [:update, :new, :create, :edit, :show]
    resources :category_item_keys, path: "keys", only: [:update, :new, :create] 
  end
end
get 'guides/:guide_id/:category_id/keys/edit', to: 'controllerName#actionName', as: :your_custom_path_name
Yang
  • 1,915
  • 1
  • 9
  • 15
0

There is a better way:

resources :guides do
  resources :categories, except: [:delete] do
    resources :items, controller: 'category_items', except: [:delete]
    resources :item_keys, controller: 'category_item_keys', only: [:update, :new, :create] do
      get :edit, to: 'category_item_keys#edit'
    end
  end
end
sidegeeks
  • 1,021
  • 3
  • 12
  • 17