Skip to content

Commit b751ead

Browse files
authored
Merge pull request #3 from typecraft-dev/ep-3-adding-crud-actions
Add all crud actions by hand for Projects
2 parents 44e13cb + 352a3b4 commit b751ead

File tree

8 files changed

+79
-2
lines changed

8 files changed

+79
-2
lines changed

app/controllers/projects_controller.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,47 @@ def index
66
def show
77
@project = Project.find(params[:id])
88
end
9+
10+
def new
11+
@project = Project.new
12+
end
13+
14+
def create
15+
@project = Project.new(project_params)
16+
17+
if @project.save
18+
redirect_to project_path(@project)
19+
else
20+
render :new, status: :unprocessable_entity
21+
end
22+
end
23+
24+
def edit
25+
@project = Project.find(params[:id])
26+
end
27+
28+
def update
29+
@project = Project.find(params[:id])
30+
31+
if @project.update(project_params)
32+
flash[:notice] = "Project updated successfully!"
33+
redirect_to project_path(@project)
34+
else
35+
render :edit, status: :unprocessable_entity
36+
end
37+
end
38+
39+
def destroy
40+
@project = Project.find(params[:id])
41+
42+
@project.destroy
43+
flash[:notice] = "Project deleted."
44+
redirect_to projects_path
45+
end
46+
47+
private
48+
49+
def project_params
50+
params.expect(project: [ :name ])
51+
end
952
end

app/models/project.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Project < ApplicationRecord
2+
validates :name, presence: { message: "Was forgotten?" }
23
end

app/views/layouts/application.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
</head>
2424

2525
<body>
26+
<% if flash[:notice] %>
27+
<p class="flash"><%= flash[:notice] %></p>
28+
<% end %>
29+
2630
<%= yield %>
2731
</body>
2832
</html>

app/views/projects/_form.html.erb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<%= form_with model: project do |f| %>
2+
3+
<% if project.errors.any? %>
4+
<div class="errors">
5+
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>
6+
<ul>
7+
<% project.errors.full_messages.each do |message| %>
8+
<li><%= message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div>
15+
<%= f.label :name %>
16+
<%= f.text_field :name %>
17+
</div>
18+
19+
<div class="actions">
20+
<%= f.submit %>
21+
</div>
22+
<% end %>

app/views/projects/edit.html.erb

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

app/views/projects/new.html.erb

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

app/views/projects/show.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
<h1><%= @project.name %></h1>
22
<p>Project details coming soon...</p>
3+
4+
<%= button_to "Delete Project", project_path(@project), method: :delete %>

config/routes.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Rails.application.routes.draw do
22
resources :todos
3+
resources :projects
34

4-
get "projects", to: "projects#index"
5-
get "projects/:id", to: "projects#show", as: "project"
65
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
76

87
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.

0 commit comments

Comments
 (0)