Skip to content

Commit

Permalink
Create Boxes
Browse files Browse the repository at this point in the history
Command line:
```
rails g scaffold box name:string description:text a_date:date a_datetime:datetime a_time:time
```
  • Loading branch information
tagliala committed May 1, 2020
1 parent 6d7bf31 commit 7e6ee88
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/boxes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the boxes controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
65 changes: 65 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
body {
background-color: #fff;
color: #333;
margin: 33px; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }

pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }

a {
color: #000; }

a:visited {
color: #666; }

a:hover {
color: #fff;
background-color: #000; }

th {
padding-bottom: 5px; }

td {
padding: 0 5px 7px; }

div.field,
div.actions {
margin-bottom: 10px; }

#notice {
color: green; }

.field_with_errors {
padding: 2px;
background-color: red;
display: table; }

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0; }

#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff; }

#error_explanation ul li {
font-size: 12px;
list-style: square; }

label {
display: block; }
74 changes: 74 additions & 0 deletions app/controllers/boxes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class BoxesController < ApplicationController
before_action :set_box, only: [:show, :edit, :update, :destroy]

# GET /boxes
# GET /boxes.json
def index
@boxes = Box.all
end

# GET /boxes/1
# GET /boxes/1.json
def show
end

# GET /boxes/new
def new
@box = Box.new
end

# GET /boxes/1/edit
def edit
end

# POST /boxes
# POST /boxes.json
def create
@box = Box.new(box_params)

respond_to do |format|
if @box.save
format.html { redirect_to @box, notice: 'Box was successfully created.' }
format.json { render :show, status: :created, location: @box }
else
format.html { render :new }
format.json { render json: @box.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /boxes/1
# PATCH/PUT /boxes/1.json
def update
respond_to do |format|
if @box.update(box_params)
format.html { redirect_to @box, notice: 'Box was successfully updated.' }
format.json { render :show, status: :ok, location: @box }
else
format.html { render :edit }
format.json { render json: @box.errors, status: :unprocessable_entity }
end
end
end

# DELETE /boxes/1
# DELETE /boxes/1.json
def destroy
@box.destroy
respond_to do |format|
format.html { redirect_to boxes_url, notice: 'Box was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_box
@box = Box.find(params[:id])
end

# Only allow a list of trusted parameters through.
def box_params
params.require(:box).permit(:name, :description, :a_date, :a_datetime, :a_time)
end
end
2 changes: 2 additions & 0 deletions app/helpers/boxes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BoxesHelper
end
2 changes: 2 additions & 0 deletions app/models/box.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Box < ApplicationRecord
end
2 changes: 2 additions & 0 deletions app/views/boxes/_box.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! box, :id, :name, :description, :a_date, :a_datetime, :a_time, :created_at, :updated_at
json.url box_url(box, format: :json)
42 changes: 42 additions & 0 deletions app/views/boxes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%= form_with(model: box, local: true) do |form| %>
<% if box.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(box.errors.count, "error") %> prohibited this box from being saved:</h2>

<ul>
<% box.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>

<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>

<div class="field">
<%= form.label :a_date %>
<%= form.date_select :a_date %>
</div>

<div class="field">
<%= form.label :a_datetime %>
<%= form.datetime_select :a_datetime %>
</div>

<div class="field">
<%= form.label :a_time %>
<%= form.time_select :a_time %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/boxes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Box</h1>

<%= render 'form', box: @box %>

<%= link_to 'Show', @box %> |
<%= link_to 'Back', boxes_path %>
35 changes: 35 additions & 0 deletions app/views/boxes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<p id="notice"><%= notice %></p>

<h1>Boxes</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>A date</th>
<th>A datetime</th>
<th>A time</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @boxes.each do |box| %>
<tr>
<td><%= box.name %></td>
<td><%= box.description %></td>
<td><%= box.a_date %></td>
<td><%= box.a_datetime %></td>
<td><%= box.a_time %></td>
<td><%= link_to 'Show', box %></td>
<td><%= link_to 'Edit', edit_box_path(box) %></td>
<td><%= link_to 'Destroy', box, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Box', new_box_path %>
1 change: 1 addition & 0 deletions app/views/boxes/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @boxes, partial: "boxes/box", as: :box
5 changes: 5 additions & 0 deletions app/views/boxes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Box</h1>

<%= render 'form', box: @box %>

<%= link_to 'Back', boxes_path %>
29 changes: 29 additions & 0 deletions app/views/boxes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @box.name %>
</p>

<p>
<strong>Description:</strong>
<%= @box.description %>
</p>

<p>
<strong>A date:</strong>
<%= @box.a_date %>
</p>

<p>
<strong>A datetime:</strong>
<%= @box.a_datetime %>
</p>

<p>
<strong>A time:</strong>
<%= @box.a_time %>
</p>

<%= link_to 'Edit', edit_box_path(@box) %> |
<%= link_to 'Back', boxes_path %>
1 change: 1 addition & 0 deletions app/views/boxes/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "boxes/box", box: @box
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :boxes
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root to: 'pages#home'
end
13 changes: 13 additions & 0 deletions db/migrate/20200501093759_create_boxes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateBoxes < ActiveRecord::Migration[6.0]
def change
create_table :boxes do |t|
t.string :name
t.text :description
t.date :a_date
t.datetime :a_datetime
t.time :a_time

t.timestamps
end
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_05_01_093759) do

create_table "boxes", force: :cascade do |t|
t.string "name"
t.text "description"
t.date "a_date"
t.datetime "a_datetime"
t.time "a_time"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

end

0 comments on commit 7e6ee88

Please sign in to comment.