Skip to content

Commit 2bc0c1c

Browse files
author
Ryan Bigg
committed
Section 4.2.2: Projects can now be updated
1 parent cdf7f62 commit 2bc0c1c

File tree

6 files changed

+64
-24
lines changed

6 files changed

+64
-24
lines changed

ticketee/app/controllers/projects_controller.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ def show
2323
@project = Project.find(params[:id])
2424
end
2525

26+
def edit
27+
@project = Project.find(params[:id])
28+
end
29+
30+
def update
31+
@project = Project.find(params[:id])
32+
33+
if @project.update(project_params)
34+
flash[:notice] = "Project has been updated."
35+
redirect_to @project
36+
else
37+
flash[:alert] = "Project has not been updated."
38+
render "edit"
39+
end
40+
end
41+
2642
private
2743

2844
def project_params
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%= form_for(@project) do |f| %>
2+
<% if @project.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(@project.errors.count, "error") %>
5+
prohibited this project from being saved:</h2>
6+
7+
<ul>
8+
<% @project.errors.full_messages.each do |msg| %>
9+
<li><%= msg %></li>
10+
<% end %>
11+
</ul>
12+
</div>
13+
<% end %>
14+
<p>
15+
<%= f.label :name %><br>
16+
<%= f.text_field :name %>
17+
</p>
18+
19+
<p>
20+
<%= f.label :description %><br>
21+
<%= f.text_field :description %>
22+
</p>
23+
24+
<%= f.submit %>
25+
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>Edit project</h2>
2+
3+
<%= render "form" %>
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
11
<h2>New Project</h2>
2-
<%= form_for(@project) do |f| %>
3-
<% if @project.errors.any? %>
4-
<div id="error_explanation">
5-
<h2><%= pluralize(@project.errors.count, "error") %>
6-
prohibited this project from being saved:</h2>
72

8-
<ul>
9-
<% @project.errors.full_messages.each do |msg| %>
10-
<li><%= msg %></li>
11-
<% end %>
12-
</ul>
13-
</div>
14-
<% end %>
15-
<p>
16-
<%= f.label :name %><br>
17-
<%= f.text_field :name %>
18-
</p>
19-
20-
<p>
21-
<%= f.label :description %><br>
22-
<%= f.text_field :description %>
23-
</p>
24-
25-
<%= f.submit %>
26-
<% end %>
3+
<%= render "form" %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<% title(@project.name, "Projects") %>
22
<h2><%= @project.name %></h2>
3+
<%= link_to "Edit Project", edit_project_path(@project) %>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "rails_helper"
2+
3+
feature "Editing Projects" do
4+
before do
5+
FactoryGirl.create(:project, name: "Sublime Text 3")
6+
7+
visit "/"
8+
click_link "Sublime Text 3"
9+
click_link "Edit Project"
10+
end
11+
12+
scenario "Updating a project" do
13+
fill_in "Name", with: "Sublime Text 3 beta"
14+
click_button "Update Project"
15+
16+
expect(page).to have_content("Project has been updated.")
17+
end
18+
end

0 commit comments

Comments
 (0)