-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from tsheporamantso/food-list
Food list 👨💻
- Loading branch information
Showing
11 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module FoodsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
db/migrate/20240206104934_remove_recipes_id_and_foods_id_from_recipe_foods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |