Skip to content

Commit 89db06d

Browse files
author
Ryan Bigg
committed
Section 1.2.4: Scaffolding
1 parent ada07d2 commit 89db06d

File tree

18 files changed

+314
-0
lines changed

18 files changed

+314
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the purchases controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
body {
2+
background-color: #fff;
3+
color: #333;
4+
font-family: verdana, arial, helvetica, sans-serif;
5+
font-size: 13px;
6+
line-height: 18px;
7+
}
8+
9+
p, ol, ul, td {
10+
font-family: verdana, arial, helvetica, sans-serif;
11+
font-size: 13px;
12+
line-height: 18px;
13+
}
14+
15+
pre {
16+
background-color: #eee;
17+
padding: 10px;
18+
font-size: 11px;
19+
}
20+
21+
a {
22+
color: #000;
23+
&:visited {
24+
color: #666;
25+
}
26+
&:hover {
27+
color: #fff;
28+
background-color: #000;
29+
}
30+
}
31+
32+
div {
33+
&.field, &.actions {
34+
margin-bottom: 10px;
35+
}
36+
}
37+
38+
#notice {
39+
color: green;
40+
}
41+
42+
.field_with_errors {
43+
padding: 2px;
44+
background-color: red;
45+
display: table;
46+
}
47+
48+
#error_explanation {
49+
width: 450px;
50+
border: 2px solid red;
51+
padding: 7px;
52+
padding-bottom: 0;
53+
margin-bottom: 20px;
54+
background-color: #f0f0f0;
55+
h2 {
56+
text-align: left;
57+
font-weight: bold;
58+
padding: 5px 5px 5px 15px;
59+
font-size: 12px;
60+
margin: -7px;
61+
margin-bottom: 0px;
62+
background-color: #c00;
63+
color: #fff;
64+
}
65+
ul li {
66+
font-size: 12px;
67+
list-style: square;
68+
}
69+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class PurchasesController < ApplicationController
2+
before_action :set_purchase, only: [:show, :edit, :update, :destroy]
3+
4+
# GET /purchases
5+
# GET /purchases.json
6+
def index
7+
@purchases = Purchase.all
8+
end
9+
10+
# GET /purchases/1
11+
# GET /purchases/1.json
12+
def show
13+
end
14+
15+
# GET /purchases/new
16+
def new
17+
@purchase = Purchase.new
18+
end
19+
20+
# GET /purchases/1/edit
21+
def edit
22+
end
23+
24+
# POST /purchases
25+
# POST /purchases.json
26+
def create
27+
@purchase = Purchase.new(purchase_params)
28+
29+
respond_to do |format|
30+
if @purchase.save
31+
format.html { redirect_to @purchase, notice: 'Purchase was successfully created.' }
32+
format.json { render :show, status: :created, location: @purchase }
33+
else
34+
format.html { render :new }
35+
format.json { render json: @purchase.errors, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
# PATCH/PUT /purchases/1
41+
# PATCH/PUT /purchases/1.json
42+
def update
43+
respond_to do |format|
44+
if @purchase.update(purchase_params)
45+
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
46+
format.json { render :show, status: :ok, location: @purchase }
47+
else
48+
format.html { render :edit }
49+
format.json { render json: @purchase.errors, status: :unprocessable_entity }
50+
end
51+
end
52+
end
53+
54+
# DELETE /purchases/1
55+
# DELETE /purchases/1.json
56+
def destroy
57+
@purchase.destroy
58+
respond_to do |format|
59+
format.html { redirect_to purchases_url, notice: 'Purchase was successfully destroyed.' }
60+
format.json { head :no_content }
61+
end
62+
end
63+
64+
private
65+
# Use callbacks to share common setup or constraints between actions.
66+
def set_purchase
67+
@purchase = Purchase.find(params[:id])
68+
end
69+
70+
# Never trust parameters from the scary internet, only allow the white list through.
71+
def purchase_params
72+
params.require(:purchase).permit(:name, :cost)
73+
end
74+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module PurchasesHelper
2+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Purchase < ActiveRecord::Base
2+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%= form_for(@purchase) do |f| %>
2+
<% if @purchase.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(@purchase.errors.count, "error") %> prohibited this purchase from being saved:</h2>
5+
6+
<ul>
7+
<% @purchase.errors.full_messages.each do |message| %>
8+
<li><%= message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= f.label :name %><br>
16+
<%= f.text_field :name %>
17+
</div>
18+
<div class="field">
19+
<%= f.label :cost %><br>
20+
<%= f.text_field :cost %>
21+
</div>
22+
<div class="actions">
23+
<%= f.submit %>
24+
</div>
25+
<% end %>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing Purchase</h1>
2+
3+
<%= render 'form' %>
4+
5+
<%= link_to 'Show', @purchase %> |
6+
<%= link_to 'Back', purchases_path %>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<h1>Listing Purchases</h1>
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Name</th>
9+
<th>Cost</th>
10+
<th colspan="3"></th>
11+
</tr>
12+
</thead>
13+
14+
<tbody>
15+
<% @purchases.each do |purchase| %>
16+
<tr>
17+
<td><%= purchase.name %></td>
18+
<td><%= purchase.cost %></td>
19+
<td><%= link_to 'Show', purchase %></td>
20+
<td><%= link_to 'Edit', edit_purchase_path(purchase) %></td>
21+
<td><%= link_to 'Destroy', purchase, method: :delete, data: { confirm: 'Are you sure?' } %></td>
22+
</tr>
23+
<% end %>
24+
</tbody>
25+
</table>
26+
27+
<br>
28+
29+
<%= link_to 'New Purchase', new_purchase_path %>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
json.array!(@purchases) do |purchase|
2+
json.extract! purchase, :id, :name, :cost
3+
json.url purchase_url(purchase, format: :json)
4+
end

0 commit comments

Comments
 (0)