Skip to content
This repository was archived by the owner on Mar 9, 2020. It is now read-only.

Commit 6fbdd63

Browse files
committed
Add project editing.
1 parent 349bf88 commit 6fbdd63

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

app/controllers/projects_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ def show
2121
@project = Project.find(params[:id])
2222
end
2323

24+
def edit
25+
@project = Project.find(params[:id])
26+
end
27+
28+
def update
29+
@project = Project.find(params[:id])
30+
@project.update(project_params)
31+
32+
if @project.save
33+
redirect_to projects_url
34+
else
35+
render :edit
36+
end
37+
end
38+
2439
def destroy
2540
@project = Project.find(params[:id])
2641
@project.destroy

app/views/projects/edit.html.erb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<h1>Edit Project</h1>
2+
3+
<%= form_for :project, url: @project, method: :put do |f| %>
4+
5+
<% if @project.errors.any? %>
6+
<div class="error_messages">
7+
<h2>Form is invalid</h2>
8+
<ul>
9+
<% for message in @project.errors.full_messages %>
10+
<li><%= message %></li>
11+
<% end %>
12+
</ul>
13+
</div>
14+
<% end %>
15+
16+
<p>
17+
<%= f.label :name %><br>
18+
<%= f.text_field :name %>
19+
</p>
20+
21+
<p>
22+
<%= f.submit %>
23+
</p>
24+
25+
<% end %>
26+

app/views/projects/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<ul>
44
<% @projects.each do |project| %>
5-
<li><%= link_to project.name, project %> | <%= link_to "Destroy", project, method: :delete %></li>
5+
<li><%= link_to project.name, project %> | <%= link_to "Edit", edit_project_path(project) %> | <%= link_to "Destroy", project, method: :delete %></li>
66
<% end %>
77
</ul>
88

app/views/projects/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1>Project "<%= @project.name %>"</h1>
22

3-
<p>A nice project.</p>
3+
<p>A nice project. <%= link_to "Edit it!", edit_project_path %></p>
44

55
<p><%= link_to 'All projects', projects_path %></p>

0 commit comments

Comments
 (0)