Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Food list πŸ‘¨β€πŸ’» #12

Merged
merged 12 commits into from
Feb 6, 2024
32 changes: 32 additions & 0 deletions app/controllers/foods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class FoodsController < ApplicationController
def index
@user = User.find(params[:user_id])
@foods = @user.foods
end

def new
@user = current_user
@food = @user.foods.build
end

def create
@food = current_user.foods.build(food_params)
if @food.save
redirect_to user_foods_path(current_user)
else
render :new, status: :unprocessable_entity
end
end

def destroy
@food = current_user.foods.find(params[:id])
@food.destroy
redirect_to user_foods_path(current_user), notice: 'Food successfully deleted'
end

private

def food_params
params.require(:food).permit(:name, :measurement_unit, :price, :quantity)
end
end
2 changes: 2 additions & 0 deletions app/helpers/foods_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module FoodsHelper
end
26 changes: 26 additions & 0 deletions app/views/foods/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Foods!</h1>

<%= link_to 'Add food', new_user_food_path(@user) %>

<table>
<thead>
<tr>
<th>Name</th>
<th>Measurement Unit</th>
<th>Price</th>
<th>Quantity</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% @foods.each do |food| %>
<tr>
<td><%= food.name %></td>
<td><%= food.measurement_unit %></td>
<td><%= food.price %></td>
<td><%= food.quantity %></td>
<td><%= button_to 'Remove', user_food_path(@user, food), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
27 changes: 27 additions & 0 deletions app/views/foods/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h1>Add food</h1>

<%= form_with model: @food, url: user_foods_path(@user), local: true, method: :post do |form| %>
<div>
<%= form.label :name %><br>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :measurement_unit %><br>
<%= form.select :measurement_unit, ['mg', 'g', 'kg', 'oz', 'lb', 'units'] %>
</div>

<div>
<%= form.label :price %><br>
<%= form.text_field :price %>
</div>

<div>
<%= form.label :quantity %><br>
<%= form.text_field :quantity %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
root to: "users#index"

resources :users, only: [:index, :show] do
resources :foods, only: [:index, :new, :create, :destroy]
resources :recipes, only:[:index, :new, :show, :create, :destroy]
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240206104401_remove_users_id_from_foods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveUsersIdFromFoods < ActiveRecord::Migration[7.1]
def change
remove_column :foods, :users_id
add_reference :foods, :user, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class RemoveRecipesIdAndFoodsIdFromRecipeFoods < ActiveRecord::Migration[7.1]
def change
remove_column :recipe_foods, :recipes_id
remove_column :recipe_foods, :foods_id
add_reference :recipe_foods, :recipe, foreign_key: true
add_reference :recipe_foods, :food, foreign_key: true
end
end
20 changes: 10 additions & 10 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions spec/helpers/foods_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the FoodsHelper. For example:
#
# describe FoodsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe FoodsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
10 changes: 10 additions & 0 deletions spec/requests/foods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe 'Foods', type: :request do
describe 'GET /index' do
it 'returns http success' do
get '/foods/index'
expect(response).to have_http_status(:success)
end
end
end
5 changes: 5 additions & 0 deletions spec/views/foods/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe 'foods/index.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading