Skip to content

Commit 7e6ee88

Browse files
committed
Create Boxes
Command line: ``` rails g scaffold box name:string description:text a_date:date a_datetime:datetime a_time:time ```
1 parent 6d7bf31 commit 7e6ee88

16 files changed

+306
-0
lines changed

app/assets/stylesheets/boxes.scss

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 boxes controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: https://sass-lang.com/

app/assets/stylesheets/scaffolds.scss

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
body {
2+
background-color: #fff;
3+
color: #333;
4+
margin: 33px; }
5+
6+
body, p, ol, ul, td {
7+
font-family: verdana, arial, helvetica, sans-serif;
8+
font-size: 13px;
9+
line-height: 18px; }
10+
11+
pre {
12+
background-color: #eee;
13+
padding: 10px;
14+
font-size: 11px; }
15+
16+
a {
17+
color: #000; }
18+
19+
a:visited {
20+
color: #666; }
21+
22+
a:hover {
23+
color: #fff;
24+
background-color: #000; }
25+
26+
th {
27+
padding-bottom: 5px; }
28+
29+
td {
30+
padding: 0 5px 7px; }
31+
32+
div.field,
33+
div.actions {
34+
margin-bottom: 10px; }
35+
36+
#notice {
37+
color: green; }
38+
39+
.field_with_errors {
40+
padding: 2px;
41+
background-color: red;
42+
display: table; }
43+
44+
#error_explanation {
45+
width: 450px;
46+
border: 2px solid red;
47+
padding: 7px 7px 0;
48+
margin-bottom: 20px;
49+
background-color: #f0f0f0; }
50+
51+
#error_explanation h2 {
52+
text-align: left;
53+
font-weight: bold;
54+
padding: 5px 5px 5px 15px;
55+
font-size: 12px;
56+
margin: -7px -7px 0;
57+
background-color: #c00;
58+
color: #fff; }
59+
60+
#error_explanation ul li {
61+
font-size: 12px;
62+
list-style: square; }
63+
64+
label {
65+
display: block; }

app/controllers/boxes_controller.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class BoxesController < ApplicationController
2+
before_action :set_box, only: [:show, :edit, :update, :destroy]
3+
4+
# GET /boxes
5+
# GET /boxes.json
6+
def index
7+
@boxes = Box.all
8+
end
9+
10+
# GET /boxes/1
11+
# GET /boxes/1.json
12+
def show
13+
end
14+
15+
# GET /boxes/new
16+
def new
17+
@box = Box.new
18+
end
19+
20+
# GET /boxes/1/edit
21+
def edit
22+
end
23+
24+
# POST /boxes
25+
# POST /boxes.json
26+
def create
27+
@box = Box.new(box_params)
28+
29+
respond_to do |format|
30+
if @box.save
31+
format.html { redirect_to @box, notice: 'Box was successfully created.' }
32+
format.json { render :show, status: :created, location: @box }
33+
else
34+
format.html { render :new }
35+
format.json { render json: @box.errors, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
# PATCH/PUT /boxes/1
41+
# PATCH/PUT /boxes/1.json
42+
def update
43+
respond_to do |format|
44+
if @box.update(box_params)
45+
format.html { redirect_to @box, notice: 'Box was successfully updated.' }
46+
format.json { render :show, status: :ok, location: @box }
47+
else
48+
format.html { render :edit }
49+
format.json { render json: @box.errors, status: :unprocessable_entity }
50+
end
51+
end
52+
end
53+
54+
# DELETE /boxes/1
55+
# DELETE /boxes/1.json
56+
def destroy
57+
@box.destroy
58+
respond_to do |format|
59+
format.html { redirect_to boxes_url, notice: 'Box 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_box
67+
@box = Box.find(params[:id])
68+
end
69+
70+
# Only allow a list of trusted parameters through.
71+
def box_params
72+
params.require(:box).permit(:name, :description, :a_date, :a_datetime, :a_time)
73+
end
74+
end

app/helpers/boxes_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module BoxesHelper
2+
end

app/models/box.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Box < ApplicationRecord
2+
end

app/views/boxes/_box.json.jbuilder

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! box, :id, :name, :description, :a_date, :a_datetime, :a_time, :created_at, :updated_at
2+
json.url box_url(box, format: :json)

app/views/boxes/_form.html.erb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<%= form_with(model: box, local: true) do |form| %>
2+
<% if box.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(box.errors.count, "error") %> prohibited this box from being saved:</h2>
5+
6+
<ul>
7+
<% box.errors.full_messages.each do |message| %>
8+
<li><%= message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= form.label :name %>
16+
<%= form.text_field :name %>
17+
</div>
18+
19+
<div class="field">
20+
<%= form.label :description %>
21+
<%= form.text_area :description %>
22+
</div>
23+
24+
<div class="field">
25+
<%= form.label :a_date %>
26+
<%= form.date_select :a_date %>
27+
</div>
28+
29+
<div class="field">
30+
<%= form.label :a_datetime %>
31+
<%= form.datetime_select :a_datetime %>
32+
</div>
33+
34+
<div class="field">
35+
<%= form.label :a_time %>
36+
<%= form.time_select :a_time %>
37+
</div>
38+
39+
<div class="actions">
40+
<%= form.submit %>
41+
</div>
42+
<% end %>

app/views/boxes/edit.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing Box</h1>
2+
3+
<%= render 'form', box: @box %>
4+
5+
<%= link_to 'Show', @box %> |
6+
<%= link_to 'Back', boxes_path %>

app/views/boxes/index.html.erb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<h1>Boxes</h1>
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Name</th>
9+
<th>Description</th>
10+
<th>A date</th>
11+
<th>A datetime</th>
12+
<th>A time</th>
13+
<th colspan="3"></th>
14+
</tr>
15+
</thead>
16+
17+
<tbody>
18+
<% @boxes.each do |box| %>
19+
<tr>
20+
<td><%= box.name %></td>
21+
<td><%= box.description %></td>
22+
<td><%= box.a_date %></td>
23+
<td><%= box.a_datetime %></td>
24+
<td><%= box.a_time %></td>
25+
<td><%= link_to 'Show', box %></td>
26+
<td><%= link_to 'Edit', edit_box_path(box) %></td>
27+
<td><%= link_to 'Destroy', box, method: :delete, data: { confirm: 'Are you sure?' } %></td>
28+
</tr>
29+
<% end %>
30+
</tbody>
31+
</table>
32+
33+
<br>
34+
35+
<%= link_to 'New Box', new_box_path %>

app/views/boxes/index.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.array! @boxes, partial: "boxes/box", as: :box

app/views/boxes/new.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>New Box</h1>
2+
3+
<%= render 'form', box: @box %>
4+
5+
<%= link_to 'Back', boxes_path %>

app/views/boxes/show.html.erb

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+
<p>
4+
<strong>Name:</strong>
5+
<%= @box.name %>
6+
</p>
7+
8+
<p>
9+
<strong>Description:</strong>
10+
<%= @box.description %>
11+
</p>
12+
13+
<p>
14+
<strong>A date:</strong>
15+
<%= @box.a_date %>
16+
</p>
17+
18+
<p>
19+
<strong>A datetime:</strong>
20+
<%= @box.a_datetime %>
21+
</p>
22+
23+
<p>
24+
<strong>A time:</strong>
25+
<%= @box.a_time %>
26+
</p>
27+
28+
<%= link_to 'Edit', edit_box_path(@box) %> |
29+
<%= link_to 'Back', boxes_path %>

app/views/boxes/show.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.partial! "boxes/box", box: @box

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
resources :boxes
23
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
34
root to: 'pages#home'
45
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class CreateBoxes < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :boxes do |t|
4+
t.string :name
5+
t.text :description
6+
t.date :a_date
7+
t.datetime :a_datetime
8+
t.time :a_time
9+
10+
t.timestamps
11+
end
12+
end
13+
end

db/schema.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is auto-generated from the current state of the database. Instead
2+
# of editing this file, please use the migrations feature of Active Record to
3+
# incrementally modify your database, and then regenerate this schema definition.
4+
#
5+
# This file is the source Rails uses to define your schema when running `rails
6+
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
7+
# be faster and is potentially less error prone than running all of your
8+
# migrations from scratch. Old migrations may fail to apply correctly if those
9+
# migrations use external dependencies or application code.
10+
#
11+
# It's strongly recommended that you check this file into your version control system.
12+
13+
ActiveRecord::Schema.define(version: 2020_05_01_093759) do
14+
15+
create_table "boxes", force: :cascade do |t|
16+
t.string "name"
17+
t.text "description"
18+
t.date "a_date"
19+
t.datetime "a_datetime"
20+
t.time "a_time"
21+
t.datetime "created_at", precision: 6, null: false
22+
t.datetime "updated_at", precision: 6, null: false
23+
end
24+
25+
end

0 commit comments

Comments
 (0)