Rail / Route Application / Management Model

I am trying to work on this Rails application that has the following goals:

/foods/- display a list of product categories (for example: Bread, Dairy products, Cookies ... etc.)
/foods/breads/- all products that are in the food category "Bread"
foods/breads/bagel- make a detailed overview of the properties of Food (in this example Bagel).

I currently have two models with associated controllers:

Foods- contains a list of products (for example, bagel, rice, toast, rich tea biscuit ... etc.) and is configured for belongs_toone food cat

Food Categories- a list of categories such as "Dairy", "Bread" ... etc. and set tohas_many :foods

I really got stuck on how to achieve my goals. I really need some advice on routing, controller actions, and views.

Any suggestions?

+3
source share
2 answers

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.

+1
source

FoodsController FoodCategoriesController, Food and FoodCategory. RESTful, , URL-, :

match '/foods' => 'food_categories#index'
match '/foods/:category_id' => 'food_categories#show'
match '/foods/:category_id/:food_id' => 'foods#show'

FoodCategoriesController index, , FoodCategory.find: , , FoodCategory :category_id has_many . FoodController show, :food_id Food. :category_id , .

0

Source: https://habr.com/ru/post/1793275/


All Articles