0

I'm looking for advice to find the ideal solution that fits within Rails' best practises.

My app has 5 models:

  • Category
  • Place
  • Division
  • Subdivision
  • Item (polymorphic)

A Place belongs to a Category. A Division belongs to a Place. A Subdivision belongs to a Division, and an Item can belong to a Place, Division OR Subdivision.

Here is my routes.rb

resources :categories, path: ''

get ':category/:place', to: 'places#show', as: :place
get ':category/:place/:item', to: 'items#show', as: :place_item
get ':category/:place/:division', to: 'divisions#show', as: :division
get ':category/:place/:division/:item', to: 'items#show', as: :division_item
get ':category/:place/:division/:subdivision', to: 'subdivisions#show', as: :subdivision
get ':category/:place/:division/:subdivision/:item', to: 'items#show', as: :subdivision_item

The issue is that if I type in the URL:

/category/place/divison

The error is "could not find ITEM".

I am currently using friendly_id for the slugs, is there a best method for introducing a new model "Slug" that would handle the slugs for all 5 of these models?

Potentially something like this? (Haven't tested)

get '/*category_id', :controller => 'slugs'

Here's item_controller:

def show
end

def show_place_item
  render "show"
end

def show_division_item
  render "show"
end

def show_subdivision_item
  render "show"
end

private
    def set_item
      @item = Item.friendly.find(params[:id])
    end

    def item_params
      params.require(:item).permit(:name, :slug, :category_id)
    end
Kobius
  • 674
  • 7
  • 28
  • How are you trying to find the item? Show some controller code. – Josh Brody Jul 28 '18 at 02:12
  • Have added controller code for the item controller, pretty out-of-the-box at this point, mostly keen on understanding the potential of creating a Slug model to handle all routes for the 5 current Models. – Kobius Jul 28 '18 at 03:41

0 Answers0