From a84b8605a1ef286e0141d963f720d30d800217fb Mon Sep 17 00:00:00 2001 From: demesameneshoa Date: Wed, 20 Dec 2023 17:27:12 +0300 Subject: [PATCH 1/5] feat: Create the Recipe List - create recipt contoller - modify the recipe model to add the necessary validation - create recipe index to display the recipes for the user - create recipe form to add new recipe --- app/controllers/recipes_controller.rb | 9 +-- app/models/recipe.rb | 6 ++ app/views/recipes/_form.html.erb | 87 ++++++++++++++------------- app/views/recipes/index.html.erb | 45 ++++++++++---- app/views/recipes/new.html.erb | 15 ++--- 5 files changed, 98 insertions(+), 64 deletions(-) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index e3e9699..8fe7767 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -3,7 +3,7 @@ class RecipesController < ApplicationController # GET /recipes or /recipes.json def index - @recipes = Recipe.all + @recipes = Recipe.where(user_id: current_user.id) end # GET /recipes/1 or /recipes/1.json @@ -20,11 +20,12 @@ def edit; end # POST /recipes or /recipes.json def create @recipe = Recipe.new(recipe_params) - + @recipe.user = current_user respond_to do |format| if @recipe.save - format.html { redirect_to recipe_url(@recipe), notice: 'Recipe was successfully created.' } - format.json { render :show, status: :created, location: @recipe } + format.html { redirect_to recipes_path, notice: 'Recipe was successfully created.' } + # format.html { redirect_to recipe_url(@recipe), notice: 'Recipe was successfully created.' } + # format.json { render :show, status: :created, location: @recipe } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @recipe.errors, status: :unprocessable_entity } diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 6afddb6..9cf025c 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -2,4 +2,10 @@ class Recipe < ApplicationRecord belongs_to :user has_many :recipe_foods has_many :foods, through: :recipe_foods + + validates :name, presence: true + validates :preparation_time, presence: true, numericality: { greater_than: 0 } + validates :cooking_time, presence: true, numericality: { greater_than: 0 } + validates :description, presence: true + validates :public, inclusion: { in: [true, false] } end diff --git a/app/views/recipes/_form.html.erb b/app/views/recipes/_form.html.erb index 0b83cec..af9586a 100644 --- a/app/views/recipes/_form.html.erb +++ b/app/views/recipes/_form.html.erb @@ -1,47 +1,50 @@ -<%= form_with(model: recipe) do |form| %> - <% if recipe.errors.any? %> -
-

<%= pluralize(recipe.errors.count, "error") %> prohibited this recipe from being saved:

- - + <%= form_with(model: recipe) do |form| %> + <% if recipe.errors.any? %> +
+

<%= pluralize(recipe.errors.count, "error") %> prohibited this recipe from being saved:

+ +
    + <% recipe.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.text_field :name, + placeholder: "Recipe Name", + aria: { label: "Recipe Name" }, + class: "form-control mb-2" %>
- <% end %> - -
- <%= form.label :name, style: "display: block" %> - <%= form.text_field :name %> -
-
- <%= form.label :preparation_time, style: "display: block" %> - <%= form.number_field :preparation_time %> -
- -
- <%= form.label :cooking_time, style: "display: block" %> - <%= form.number_field :cooking_time %> -
- -
- <%= form.label :description, style: "display: block" %> - <%= form.text_area :description %> -
+
+ <%= form.text_field :preparation_time, + placeholder: "Preparation Time", + aria: { label: "Preparation Time" }, + class: "form-control mb-2" %> +
-
- <%= form.label :public, style: "display: block" %> - <%= form.check_box :public %> -
+
+ <%= form.text_field :cooking_time, + placeholder: "Cooking Time", + aria: { label: "Cooking Time" }, + class: "form-control mb-2" %> +
-
- <%= form.label :user_id, style: "display: block" %> - <%= form.text_field :user_id %> -
+
+ <%= form.text_area :description, + placeholder: "Description", + aria: { label: "Description" }, + class: "form-control mb-2" %> +
-
- <%= form.submit %> -
-<% end %> +
+ <%= form.check_box :public, class: "custom-control-input", id:"publicswitch" %> + +
+
+
+ <%= form.submit "Save", class: "btn btn-success w-100" %> +
+ <% end %> diff --git a/app/views/recipes/index.html.erb b/app/views/recipes/index.html.erb index f1d03d6..606eac6 100644 --- a/app/views/recipes/index.html.erb +++ b/app/views/recipes/index.html.erb @@ -1,14 +1,37 @@ -

<%= notice %>

+
-

Recipes

+
+
+ <%= link_to "New Recipe", new_recipe_path, class: "btn btn-outline-primary m-1" %> +
+ <% if @recipes.empty? %> +
+

There are no recipes to show.

+
+ <% else %> + +
    + <% @recipes.each_with_index do |recipe, index| %> +
  • +
    -
    - <% @recipes.each do |recipe| %> - <%= render recipe %> -

    - <%= link_to "Show this recipe", recipe %> -

    - <% end %> -
    +
    + <%= link_to recipe_path(id: recipe.id) do %> +

    Recipe <%= index + 1 %>

    + <% end %> +
    +

    Name: <%= recipe.name %>

    + <%= button_to 'Remove', recipe_path(recipe), method: :delete, + class: "btn btn-primary" %> + +
    +
    + Description: <%= recipe.description %> +
    +
  • + <% end %> + <% end %> +
+
+
-<%= link_to "New recipe", new_recipe_path %> diff --git a/app/views/recipes/new.html.erb b/app/views/recipes/new.html.erb index f125426..3def126 100644 --- a/app/views/recipes/new.html.erb +++ b/app/views/recipes/new.html.erb @@ -1,9 +1,10 @@ -

New recipe

+

New recipe

+
+ <%= render "form", recipe: @recipe %> -<%= render "form", recipe: @recipe %> +
-
- -
- <%= link_to "Back to recipes", recipes_path %> -
+
+ <%= link_to "Back to recipes list", recipes_path, class: "btn btn-secondary px-2 py-1" %> +
+
\ No newline at end of file From ccf65b99b330bdccf77ccdf167bbf34d5cbc7a33 Mon Sep 17 00:00:00 2001 From: demesameneshoa Date: Wed, 20 Dec 2023 17:30:56 +0300 Subject: [PATCH 2/5] added units to food form unit of measurement and add routes path on navbar --- app/views/foods/_form.html.erb | 2 +- app/views/layouts/_navbar.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/foods/_form.html.erb b/app/views/foods/_form.html.erb index 6533093..2afed05 100644 --- a/app/views/foods/_form.html.erb +++ b/app/views/foods/_form.html.erb @@ -20,7 +20,7 @@
<%= form.select :measurement_unit, - options_for_select(["mg", "g", "kg", "l", "ml"]), + options_for_select(["mg", "g", "kg", "l", "ml","units"]), { prompt: "Select Measurement Unit" }, aria: { label: "Measurement unit" }, class: "form-control mb-2" %> diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index ccc5074..fc11f6c 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -19,7 +19,7 @@ <%= link_to "My-recipes", root_path, class: "nav-link" %>