In the routes.rb file, I would do the following:
match 'foods' => 'FoodCategories#index'
match 'foods/:category' => 'Foods#index'
match 'foods/:category/:name' => 'Foods#show'
Then I created a product area by category:
class Food
scope :by_category, lambda{ |category| joins(:categories).where('categories.name = ?', category) }
end
Then I would have 2 actions in your FoodsController:
class FoodsController
def index
@foods = Food.by_category(params[:category])
end
def show
@foods = Food.by_category(params[:category]).where('foods.name = ?', params[:name])
end
end
And one action in your FoodCategoriesController:
class FoodCategories
def index
@categories = Category.where(name: params[:category])
end
end
This should leave you with the opportunity to implement 3 types: categories / index, products / index and products / shows.
source
share