From 7e6ee88e247b14d6fa24e5b9a7ec468bb87c5178 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 1 May 2020 11:38:48 +0200 Subject: [PATCH] Create Boxes Command line: ``` rails g scaffold box name:string description:text a_date:date a_datetime:datetime a_time:time ``` --- app/assets/stylesheets/boxes.scss | 3 + app/assets/stylesheets/scaffolds.scss | 65 ++++++++++++++++++++ app/controllers/boxes_controller.rb | 74 +++++++++++++++++++++++ app/helpers/boxes_helper.rb | 2 + app/models/box.rb | 2 + app/views/boxes/_box.json.jbuilder | 2 + app/views/boxes/_form.html.erb | 42 +++++++++++++ app/views/boxes/edit.html.erb | 6 ++ app/views/boxes/index.html.erb | 35 +++++++++++ app/views/boxes/index.json.jbuilder | 1 + app/views/boxes/new.html.erb | 5 ++ app/views/boxes/show.html.erb | 29 +++++++++ app/views/boxes/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20200501093759_create_boxes.rb | 13 ++++ db/schema.rb | 25 ++++++++ 16 files changed, 306 insertions(+) create mode 100644 app/assets/stylesheets/boxes.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/boxes_controller.rb create mode 100644 app/helpers/boxes_helper.rb create mode 100644 app/models/box.rb create mode 100644 app/views/boxes/_box.json.jbuilder create mode 100644 app/views/boxes/_form.html.erb create mode 100644 app/views/boxes/edit.html.erb create mode 100644 app/views/boxes/index.html.erb create mode 100644 app/views/boxes/index.json.jbuilder create mode 100644 app/views/boxes/new.html.erb create mode 100644 app/views/boxes/show.html.erb create mode 100644 app/views/boxes/show.json.jbuilder create mode 100644 db/migrate/20200501093759_create_boxes.rb create mode 100644 db/schema.rb diff --git a/app/assets/stylesheets/boxes.scss b/app/assets/stylesheets/boxes.scss new file mode 100644 index 0000000..149414f --- /dev/null +++ b/app/assets/stylesheets/boxes.scss @@ -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/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..bb2597f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -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; } diff --git a/app/controllers/boxes_controller.rb b/app/controllers/boxes_controller.rb new file mode 100644 index 0000000..a7b545e --- /dev/null +++ b/app/controllers/boxes_controller.rb @@ -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 diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb new file mode 100644 index 0000000..17fa1bf --- /dev/null +++ b/app/helpers/boxes_helper.rb @@ -0,0 +1,2 @@ +module BoxesHelper +end diff --git a/app/models/box.rb b/app/models/box.rb new file mode 100644 index 0000000..533e22a --- /dev/null +++ b/app/models/box.rb @@ -0,0 +1,2 @@ +class Box < ApplicationRecord +end diff --git a/app/views/boxes/_box.json.jbuilder b/app/views/boxes/_box.json.jbuilder new file mode 100644 index 0000000..76e5267 --- /dev/null +++ b/app/views/boxes/_box.json.jbuilder @@ -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) diff --git a/app/views/boxes/_form.html.erb b/app/views/boxes/_form.html.erb new file mode 100644 index 0000000..cb05898 --- /dev/null +++ b/app/views/boxes/_form.html.erb @@ -0,0 +1,42 @@ +<%= form_with(model: box, local: true) do |form| %> + <% if box.errors.any? %> +
+

<%= pluralize(box.errors.count, "error") %> prohibited this box from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :name %> + <%= form.text_field :name %> +
+ +
+ <%= form.label :description %> + <%= form.text_area :description %> +
+ +
+ <%= form.label :a_date %> + <%= form.date_select :a_date %> +
+ +
+ <%= form.label :a_datetime %> + <%= form.datetime_select :a_datetime %> +
+ +
+ <%= form.label :a_time %> + <%= form.time_select :a_time %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/boxes/edit.html.erb b/app/views/boxes/edit.html.erb new file mode 100644 index 0000000..d7e85ab --- /dev/null +++ b/app/views/boxes/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Box

+ +<%= render 'form', box: @box %> + +<%= link_to 'Show', @box %> | +<%= link_to 'Back', boxes_path %> diff --git a/app/views/boxes/index.html.erb b/app/views/boxes/index.html.erb new file mode 100644 index 0000000..3d95fe4 --- /dev/null +++ b/app/views/boxes/index.html.erb @@ -0,0 +1,35 @@ +

<%= notice %>

+ +

Boxes

+ + + + + + + + + + + + + + + <% @boxes.each do |box| %> + + + + + + + + + + + <% end %> + +
NameDescriptionA dateA datetimeA time
<%= box.name %><%= box.description %><%= box.a_date %><%= box.a_datetime %><%= box.a_time %><%= link_to 'Show', box %><%= link_to 'Edit', edit_box_path(box) %><%= link_to 'Destroy', box, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Box', new_box_path %> diff --git a/app/views/boxes/index.json.jbuilder b/app/views/boxes/index.json.jbuilder new file mode 100644 index 0000000..7b1d4c2 --- /dev/null +++ b/app/views/boxes/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @boxes, partial: "boxes/box", as: :box diff --git a/app/views/boxes/new.html.erb b/app/views/boxes/new.html.erb new file mode 100644 index 0000000..0aeb5e7 --- /dev/null +++ b/app/views/boxes/new.html.erb @@ -0,0 +1,5 @@ +

New Box

+ +<%= render 'form', box: @box %> + +<%= link_to 'Back', boxes_path %> diff --git a/app/views/boxes/show.html.erb b/app/views/boxes/show.html.erb new file mode 100644 index 0000000..b4c54f7 --- /dev/null +++ b/app/views/boxes/show.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

+ Name: + <%= @box.name %> +

+ +

+ Description: + <%= @box.description %> +

+ +

+ A date: + <%= @box.a_date %> +

+ +

+ A datetime: + <%= @box.a_datetime %> +

+ +

+ A time: + <%= @box.a_time %> +

+ +<%= link_to 'Edit', edit_box_path(@box) %> | +<%= link_to 'Back', boxes_path %> diff --git a/app/views/boxes/show.json.jbuilder b/app/views/boxes/show.json.jbuilder new file mode 100644 index 0000000..70fea38 --- /dev/null +++ b/app/views/boxes/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "boxes/box", box: @box diff --git a/config/routes.rb b/config/routes.rb index c5194c5..b687dd5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20200501093759_create_boxes.rb b/db/migrate/20200501093759_create_boxes.rb new file mode 100644 index 0000000..1cf4af0 --- /dev/null +++ b/db/migrate/20200501093759_create_boxes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..759215b --- /dev/null +++ b/db/schema.rb @@ -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