Skip to content

Commit 124fd4e

Browse files
committed
Add Recipe CRUD views, routes and controller actions
This commit creates the UI interactions in the application for managing recipes, and authenticates and restricts access to recipes to logged in users.
1 parent 84712fd commit 124fd4e

File tree

12 files changed

+141
-9
lines changed

12 files changed

+141
-9
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ group :development, :test do
1414
gem 'byebug', platform: :mri
1515
gem 'minitest-rails'
1616
gem 'factory_girl_rails'
17+
gem 'faker'
1718
end
1819

1920
group :development do

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ GEM
5757
factory_girl_rails (4.7.0)
5858
factory_girl (~> 4.7.0)
5959
railties (>= 3.0.0)
60+
faker (1.6.6)
61+
i18n (~> 0.5)
6062
ffi (1.9.14)
6163
globalid (0.3.7)
6264
activesupport (>= 4.1.0)
@@ -153,6 +155,7 @@ DEPENDENCIES
153155
byebug
154156
clearance (~> 1.15.1)
155157
factory_girl_rails
158+
faker
156159
jquery-rails
157160
listen (~> 3.0.5)
158161
minitest-rails

app/controllers/recipes_controller.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class RecipesController < ApplicationController
2+
before_action :require_login
3+
4+
def index
5+
@recipes = current_user.recipes.order(:id)
6+
end
7+
8+
def show
9+
@recipe = current_user.recipes.find(params[:id])
10+
end
11+
12+
def new
13+
@recipe = current_user.recipes.build
14+
end
15+
16+
def create
17+
@recipe = current_user.recipes.build(recipe_params)
18+
19+
if @recipe.save
20+
redirect_to recipe_path(@recipe), notice: "Recipe Created!"
21+
else
22+
@errors = @recipe.errors.full_messages
23+
render :new
24+
end
25+
end
26+
27+
def edit
28+
@recipe = current_user.recipes.find(params[:id])
29+
end
30+
31+
def update
32+
@recipe = current_user.recipes.find(params[:id])
33+
34+
if @recipe.update_attributes(recipe_params)
35+
redirect_to recipe_path(@recipe), notice: "Recipe Updated!"
36+
else
37+
@errors = @recipe.errors.full_messages
38+
render :edit
39+
end
40+
end
41+
42+
def destroy
43+
recipe = current_user.recipes.find(params[:id])
44+
recipe.destroy
45+
redirect_to recipes_path, notice: "Deleted Recipe: #{recipe.name}"
46+
end
47+
48+
private
49+
50+
def recipe_params
51+
params.require(:recipe).permit(:name, :description)
52+
end
53+
end

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class User < ApplicationRecord
22
include Clearance::User
3+
4+
has_many :recipes
35
end

app/views/recipes/_form.html.erb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<% if @errors.present? %>
2+
<div class="errors">
3+
<ul>
4+
<% @errors.each do |error| %>
5+
<li><%= error %></li>
6+
<% end %>
7+
</ul>
8+
</div>
9+
<% end %>
10+
11+
<%= form_for @recipe do |form| %>
12+
<div class="form-group">
13+
<%= form.label :name %>
14+
<%= form.text_field :name %>
15+
</div>
16+
17+
<div class="form-group">
18+
<%= form.label :description %>
19+
<%= form.text_area :description %>
20+
</div>
21+
22+
<div class="actions">
23+
<%= form.submit %>
24+
</div>
25+
<% end %>

app/views/recipes/edit.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Edit Recipe</h1>
2+
3+
<%= render partial: "form" %>

app/views/recipes/index.html.erb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<h1>Recipes</h1>
2+
3+
<div>
4+
<%= link_to "New Recipe", new_recipe_path %>
5+
</div>
6+
7+
<div class="recipes">
8+
<% @recipes.each do |recipe| %>
9+
<div class="recipe">
10+
<h3><%= link_to recipe.name, recipe_path(recipe) %></h3>
11+
<p><%= recipe.description %></p>
12+
<ul class="recipe-actions">
13+
<li><%= link_to "Edit", edit_recipe_path(recipe) %></li>
14+
<li><%= link_to "Delete", recipe_path(recipe), data: {
15+
confirm: "Are you sure you want to delete: #{recipe.name}?",
16+
}, method: :delete %></li>
17+
</ul>
18+
</div>
19+
<% end %>
20+
</div>

app/views/recipes/new.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>New Recipe</h1>
2+
3+
<%= render partial: "form" %>

app/views/recipes/show.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h1><%= @recipe.name %></h1>
2+
3+
<h2>Description:</h2>
4+
<p><%= @recipe.description %></p>
5+
6+
<div>
7+
<ul class="recipe-actions">
8+
<li><%= link_to "Back", recipes_path %></li>
9+
<li><%= link_to "Edit", edit_recipe_path(@recipe) %></li>
10+
<li><%= link_to "Delete", recipe_path(@recipe), data: {
11+
confirm: "Are you sure you want to delete: #{@recipe.name}?",
12+
}, method: :delete %></li>
13+
</ul>
14+
</div>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Rails.application.routes.draw do
22
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
33
root to: "welcome#show"
4+
5+
resources :recipes
46
end

db/seeds.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
# This file should contain all the record creation needed to seed the database with its default values.
2-
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3-
#
4-
# Examples:
5-
#
6-
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7-
# Character.create(name: 'Luke', movie: movies.first)
1+
exit if !Rails.env.development?
2+
3+
puts "Deleting the Data"
4+
Recipe.delete_all
5+
User.delete_all
6+
7+
puts "Creating User"
8+
user = FactoryGirl.create(:user, email: "test@example.com")
9+
10+
puts "Creating Recipes"
11+
20.times do
12+
FactoryGirl.create(:recipe, user: user)
13+
end

test/factories/recipes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FactoryGirl.define do
22
factory :recipe do
3-
name "Mom's Spaghetti"
4-
description "The best pasta in the world."
3+
name { Faker::Hipster.sentence }
4+
description { Faker::Hipster.paragraph }
55
association(:user)
66
end
77
end

0 commit comments

Comments
 (0)