shimmyShow Webアプリ開発ブログ

プログラミングスクールTECH::CAMPを73期を卒業し、紙媒体で実施されている教育の根本を変えていくためのサービスを開発中。当ブログを読めば誰でも当該サービスを開発できるようにするため軌跡を記録中。

【第5章】オンライン教科書サービス作成【教科書の編集機能】

目的

第4章によって教科書の表示ページを作成し教科書を表示できるようになったため、本章で投稿したテキストの編集機能を実装する。

ルーティング設定

config/routes.rbを下記のように実装する。

Rails.application.routes.draw do
  root to: 'texts#index'
  resources :texts, only: [:new, :create, :show, :edit]
end

コントローラーの実装

上記にてeditアクションのルーティングを設定したので、コントローラーにeditアクションを実装していく。
app/controllers/texts_controller.rbを下記のように実装する。

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

  def new
    @text = Text.new
  end

  def create
    Text.create(text_params)
    redirect_to root_path
  end

  def show
    @text = Text.find(params[:id])
  end

  def edit
    @text = Text.find(params[:id])
  end

  private
  def text_params
    params.require(:text).permit(:title, :abstract, :body).merge(public_flag: "1")
  end

end

ビューファイル作成

editアクションをコントローラーに実装したので、対応するビューファイルを作成する。
app/views/texts/edit.html.erb

<div class="content__title">
  テキストの編集
</div>

<%= form_with model: @text, class: :form, local: true do |form| %>
  <%= form.text_field :title, placeholder: :テキストタイトル, class: :form__title %>
  <%= form.text_area  :abstract, placeholder: :テキスト概要, class: :form__abstract %>
  <%= form.text_area :body, placeholder: :テキスト本文, class: :form__body %>
  <%= form.submit '作成する', class: :form__btn %>
<% end %>

編集ページへ遷移するリンクを実装する。
app/views/texts/show.html.erb

<div class="textTitle">
  <%= @text.title %>
</div>
<div class="textDate">
  <%= @text.created_at %>
</div>
<div class="textAuthor">
</div>
<div class="textManage">
  <%= link_to "テキスト編集", edit_text_path(@text.id), class: "textManage__edit" %>
</div>
<div class="textText">
  <%= simple_format @text.body %>
</div>

個別の教科書ページからテキスト編集ボタンを押下し、 下図のように編集画面が表示されれば成功です。
f:id:erwinmarvin:20200917223001p:plain ただし、作成するボタンを押下すると下図エラーが発生する。
f:id:erwinmarvin:20200917223146p:plain

ルーティング設定

config/routes.rbを下記のように実装する。

Rails.application.routes.draw do
  root to: 'texts#index'
  resources :texts, only: [:new, :create, :show, :edit, :update]
end

コントローラーの実装

上記にてupdateアクションのルーティングを設定したので、コントローラーにupdateアクションを実装していく。
app/controllers/texts_controller.rbを下記のように実装する。

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

  def new
    @text = Text.new
  end

  def create
    Text.create(text_params)
    redirect_to root_path
  end

  def show
    @text = Text.find(params[:id])
  end

  def edit
    @text = Text.find(params[:id])
  end

  def update
    text = Text.find(params[:id])
    text.update(text_params)
    redirect_to text_path(text.id)
  end

  private
  def text_params
    params.require(:text).permit(:title, :abstract, :body).merge(public_flag: "1")
  end

end

レコードの更新後、各教科書ページに直接リダイレクトするよう実装しています。
updateアクションに関しては、update.html.erbのようなビューは作成しない。
updateアクションを追加後、編集後、作成するボタンを押下してもエラーが発生せず、下図のように表示されていれば成功です。
f:id:erwinmarvin:20200917223901p:plain

編集機能が完了したため、次章では削除機能について説明する。
お疲れ様でした!!!

【第4章】オンライン教科書サービス作成【個別教科書の表示】

目的

第3章では教科書の投稿機能を実装した。本章では投稿した教科書の表示機能を実装し、個別の教科書を表示できるようにする。

ルーティング設定

showアクションへのルーティングを設定する。 config/routes.rbを下記のように実装する。

Rails.application.routes.draw do
  root to: 'texts#index'
  resources :texts, only: [:new, :create, :show]
end

コントローラーの実装

上記にてshowアクションのルーティングを設定したので、コントローラーにshowアクションを実装していく。
app/controllers/texts_controller.rbを下記のように実装する。

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

  def new
    @text = Text.new
  end

  def create
    Text.create(text_params)
    redirect_to root_path
  end

  def show
    @text = Text.find(params[:id])
  end

  private
  def text_params
    params.require(:text).permit(:title, :abstract, :body).merge(public_flag: "1")
  end

end

ビューファイル作成

showアクションをコントローラーに実装したので、対応するビューファイルを作成する。
app/views/texts/show.html.erb

<div class="textTitle">
  <%= @text.title %>
</div>
<div class="textDate">
  <%= @text.created_at %>
</div>
<div class="textAuthor">
</div>
<div class="textText">
  <%= simple_format @text.body %>
</div>

トップページから各教科書のページへ遷移できるようにリンクを実装する。
app/views/texts/index.html.erb

<% @texts.each do |text| %>
  <div class="content">
    <div class="content__left">
      <div class="content__left--image"></div>
    </div>
    <div class="content__right">
      <div class="content__right__top">
        <%= link_to text.title, text_path(text.id), class: "content__right__top--title" %>
      </div>
      <div class="content__right__bottom">
        <div class="content__right__bottom--date">
          <%= text.abstract %><br>
          <%= text.body %>
          <br><br>
        </div>
      </div>
    </div>
  </div>
<% end %>

下図のように表示されれば成功です。
f:id:erwinmarvin:20200917221333p:plain

教科書の表示機能の基本的な部分は完了です。
次の章で投稿した教科書の編集や削除機能を説明いたします。

【第3章】オンライン教科書サービス作成【教科書の投稿機能】

目的

第2章ではデータベースにseeds.rbを用いて仮データ登録をしたが、本章では教科書の投稿ページを作成し教科書を投稿できるようにする。

ルーティング設定

config/routes.rbを下記のように実装する。

Rails.application.routes.draw do
  root to: 'texts#index'
  resources :texts, only: [:new] 
end

コントローラーの実装

上記にてnewアクションのルーティングを設定したので、コントローラーにnewアクションを実装していく。
app/controllers/texts_controller.rbを下記のように実装する。

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

  def new
  end
end

ビューファイル作成

newアクションをコントローラーに実装したので、対応するビューファイルを作成する。
app/views/texts/new.html.erb

<div class="content__title">
  新規テキストの作成
</div>

下図のように表示されれば成功です。

f:id:erwinmarvin:20200916215439p:plain
new.html.erb

トップページに新規テキスト作成のリンクを設定

トップページから新規投稿ページにアクセスできるよう、application.html.erb実装する。
app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>OnlineText</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <header class="header">
      <div class="header__title">
        <%= link_to "OnlineText", root_path, class: "header__title--text" %>
      </div>
      <div class="header__right">
        <%= link_to "新規テキスト作成", new_text_path, class: "header__right--btn" %>
      </div>
    </header>
    <div class="contents">
      <%= yield %>
    </div>
  </body>
</html>

link_to部分の編集を実施した。
ロゴをトップページへ遷移するようroot_pathを設定。
新規テキスト作成ボタンを押下した場合に、新規テキスト作成画面へ遷移するようにnew_text_pathを設定。

インスタンスを生成と新規テキスト作成フォームの作成

app/controllers/texts_controller.rbを下記のように編集する。

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

  def new
    @text = Text.new
  end

end

app/views/texts/new.html.erbを下記のように編集する。

<div class="content__title">
  新規テキストの作成
</div>

<%= form_with model: @text, class: :form, local: true do |form| %>
  <%= form.text_field :title, placeholder: :テキストタイトル, class: :form__title %>
  <%= form.text_area  :abstract, placeholder: :テキスト概要, class: :form__abstract %>
  <%= form.text_area :body, placeholder: :テキスト本文, class: :form__body %>
  <%= form.submit '作成する', class: :form__btn %>
<% end %>

createアクションの記述をしていないため、下図エラーが発生する。
form_withは自動で次アクションへパスする。
今回の場合、newアクションの次アクションcreateが存在していていないため次がないよ。。。というエラーが発生する。

f:id:erwinmarvin:20200916221746p:plain
error

ルーティング設定

config/routes.rbへ下記のようにcreateを追加する。

Rails.application.routes.draw do
  root to: 'texts#index'
  resources :texts, only: [:new, :create]
end

コントローラーの実装

上記にてcreateアクションのルーティングを設定したので、コントローラーにcreateアクションを実装していく。 createアクションの実装に伴い、ストロングパラメータの実装も行う。
app/controllers/texts_controller.rb

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

  def new
    @text = Text.new
  end

  def create
    Text.create(text_params)
  end

  private
  def text_params
    params.require(:text).permit(:title, :abstract, :body).merge(public_flag: "1")
  end

end

対象のcssファイルも修正する。
app/assets/stylesheets/texts.scss

body{
  background-color: #f6f6f6;
  min-width: 650px;
  .header{
    height: 56px;
    width: 100%;
    padding: 0 150px;
    background-color: #66FFCC;
    color: #fff;
    display: flex;
    justify-content: space-between;
    &__title{
      font-size: 30px;
      line-height: 56px;
      &--text{
        text-decoration: none;
        color: #fff;
      }
    }
    &__right{
      &--btn{
        text-decoration: none;
        line-height: 56px;
        color: #fff;
        margin-left:15px;
      }
    }
  }
  .contents{
    margin: 30px auto 0;
    background-color: #fff;
    height: 600px;
    width: 70%;
    overflow: scroll;
    .content{
      padding: 10px;
      border-bottom: 1px solid #f6f6f6;
      display: flex;
      &__left{
        padding-right: 20px;
        &--image{
          height: 55px;
          width: 55px;
          border: solid 1px;
        }
      }
      &__right{
        width: calc(100%-100px);
        &__top{
          font-size: 20px;
          font-weight: bold;
          &--title{
            text-decoration: none;
            color: #000000;
          }
        }
        &__bottom{
          font-size: 15px;
          color: #999;
          display: flex;
          &--userName{
            &--btn{
              text-decoration: none;
              color: #999;
            }
          }
          &--date{
            margin-left: 20px;
          }
        }
      }
      &__title{
        font-size: 30px;
        font-weight: bold;
        padding-top: 30px;
        text-align: center;
      }
    }
    .textTitle{
      padding-top: 20px;
      text-align: center;
      font-size: 20px;
      font-weight: bold;
    }
    .textDate{
      font-size: 15px;
      color: #999;
      text-align: center;
    }
    .textAuthor{
      font-size: 15px;
      color: #999;
      text-align: center;
    }
    .textManage{
      display: flex;
      justify-content: center;
      margin-top: 10px;
      &__edit{
        display: block;
        border-radius: 5px;
        color: #ffffff;
        background-color:#66FFCC;
        padding: 0 10px;
        text-decoration: none;
        margin-right: 10px;
      }
      &__delete{
        display: block;
        border-radius: 5px;
        color: #ffffff;
        background-color:#66FFCC;
        padding: 0 10px;
        text-decoration: none;
        margin-left: 10px;
      }
    }
    .textText{
      margin: 20px 30px;
    }
  }
  .form{
    width: 85%;
    height: 100%;
    margin: 0 auto;
    padding-top: 30px;
    &__title{
      display: block;
      width: 100%;
      height: 50px;
    }
    &__abstract{
      display: block;
      width: 100%;
      height: 50px;
      margin-top: 15px;
    }
    &__body{
      display: block;
      width: 100%;
      height: 50%;
      margin-top: 15px;
    }
    &__btn{
      display: block;
      width: 30%;
      margin: 15px auto 0;
    }
  }
  .user{
    padding: 15px 30px;
  }
}

下図のように表示されれば成功です。 f:id:erwinmarvin:20200916223256p:plain f:id:erwinmarvin:20200916223325p:plain

バリデーションの設定

データを登録する際に、制約をかけることをバリデーションといいます。
入力必須の項目に対して空のデータを登録できないようにしたりするために用います。
普段様々なサイトで必須項目ですというエラーメッセージを見たら、バリデーションに引っ掛かったのだなと思ってください。
下記モデルの編集をします。 app/models/text.rb

class Text < ApplicationRecord
  validates :title, :abstract, :body, presence: true
end

教科書の投稿機能の基本的な部分は完了です。
次の章で投稿した教科書の表示機能を説明いたします。

【第2章】オンライン教科書サービス作成【データの表示】

目的

第1章で作成したデータベースにテーブルを作成し、下図のようにデータを表示できるようにする。

f:id:erwinmarvin:20200914223811p:plain
トップ画面

ルーティング設定

config/routes.rbを下記のように実装する。

Rails.application.routes.draw do
  root to: 'texts#index'
end

コントローラー生成

コントローラーを生成するには、rails g controller <コントローラー名>を実行する。
ターミナル

cd online_text
rails g controller texts

上記コマンド実行後、app/controllers/texts_controller.rbが生成される。
生成されたコントローラーを下記のように実装する。

class TextsController < ApplicationController

  def index
  end

end

モデル生成・マイグレーションファイルの編集

textsテーブルとやりとりするTextモデルを下記コマンドを実行して作成する。
ターミナル

cd online_text
rails g model text

上記コマンドを実行するとマイグレーションファイルが生成される。
DB設計書(ER図)に合わせて項目を追加する。
外部キーの項目に関しては対象テーブルが存在しない場合、エラーを発生するため、
対象テーブル生成後、外部キーを追加するマイグレーションファイルを作成する方針で進める。
db/migrate/20200914135559_create_texts.rb

class CreateTexts < ActiveRecord::Migration[5.2]
  def change
    create_table :texts do |t|
      t.string     :title
      t.string     :thumbnail
      t.string     :abstract
      t.text       :body, limit: 4294967295
      t.integer    :public_flag, null: false, default: 0
      t.timestamps
    end
  end
end

f:id:erwinmarvin:20200914230527p:plain
DB設計書(ER図)

下記コマンドを実行してテーブルを作成する。
ターミナル

cd online_text
rails db:migrate

下図のようにSequel Proで生成したテーブルにアクセスし、対象のカラムが存在することを確認する。

f:id:erwinmarvin:20200914231353p:plain
テーブル確認

仮データの導入

ビューで表示するための仮データをseeds.rbを用いて登録する。
カラム:thumbnailについては画像投稿のgemを追加してからseeds.rbを再度編集する。 db/seeds.rb

#######################################
# テキストテーブルへのデータ登録
#######################################
p "create Texts"
Text.create!(
  title: '集合と論理',
  # thumbnail: File.open("#{Rails.root}/db/fixtures/product1.jpeg"),
  abstract: '集合と倫理について理解する',
  body: '数学では、ある集まりのうち、さらに、それに属しているか属してないかを明確に区別できる条件のある物のあつまりを集合という。',
  public_flag: 1
)
Text.create!(
  title: '二次関数',
  # thumbnail: File.open("#{Rails.root}/db/fixtures/product1.jpeg"),
  abstract: '二次関数について理解する',
  body: '係数 a, b, c が実数値の定数で、x が実数値をとる変数とすると、そのグラフはxy-座標系において放物線を描く。',
  public_flag: 1
)

下記コマンドを実行してTextテーブルへ仮データを登録する。
ターミナル

cd online_text
rails db:seed

無事登録が完了するとテーブルが下図のようになる。

f:id:erwinmarvin:20200915212745p:plain
仮データ登録

コントローラーへ追記

テキストからデータを取得できるようにコントローラーに下記記述を加える。
app/controllers/texts_controller.rb

class TextsController < ApplicationController

  def index
    @texts = Text.all
  end

end

ビューファイルの実装

texts_controllerの@textsにはtextsテーブルの全レコードが格納されている。
@textsの情報をトップページへ表示するため、ビューファイルindex.html.erbを作成し、実装を進める。
app/views/texts/index.html.erbを作成し、下記のように記述する。

<% @texts.each do |text| %>
  <div class="content">
    <div class="content__left">
      <div class="content__left--image"></div>
    </div>
    <div class="content__right">
      <div class="content__right__top">
        <%= text.title %>
      </div>
      <div class="content__right__bottom">
        <div class="content__right__bottom--date">
          <%= text.abstract %><br>
          <%= text.body %>
          <br><br>
        </div>
      </div>
    </div>
  </div>
<% end %>

下記のように表示されていれば成功

f:id:erwinmarvin:20200915214950p:plain
view

テンプレートの導入

アプリケーションのヘッダー部分は、トップ画面、投稿画面など画面遷移した先でも同じものを使用することが多い。
アプリケーションのビューファイルの共通部分をまとめて実装するのがレイアウトテンプレートと呼ばれるファイルである。
対象ファイルはapp/views/layouts/application.html.erbであり、下記のように編集をする。

<!DOCTYPE html>
<html>
  <head>
    <title>OnlineText</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <header class="header">
      <div class="header__Ïtitle">
        <%= link_to "OnlineText", '#', class: "header__title--text" %>
      </div>
      <div class="header__right">
        <%= link_to "新規テキスト作成", '#', class: "header__right--btn" %>
      </div>
    </header>
    <div class="contents">
      <%= yield %>
    </div>
  </body>
</html>

下記のように表示されれば成功です。

f:id:erwinmarvin:20200915215859p:plain
レイアウトテンプレート

CSSの実装

レイアウトテンプレートの中に下記記述がある。
stylesheet_link_tag 上記は呼び出すCSSファイルの指定が可能になるメソッドである。 applicationと記述されているため、このHTMLファイルからはapp/assets/stylesheets/application.cssというCSSファイルを読み込んでいることを表す。
app/assets/stylesheets/application.cssは下記のように記述されている。

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
 * vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require_tree .
 *= require_self
 */

上記ファイルのrequire_tree .app/assets/stylesheetsというディレクトリにあるCSSファイルやSCSSファイルを全て読み込むという記述である。

texts.scssを下記のように記述する。 app/assets/stylesheets/texts.scss

body{
  background-color: #f6f6f6;
  min-width: 650px;
  .header{
    height: 56px;
    width: 100%;
    padding: 0 150px;
    background-color: #66FFCC;
    color: #fff;
    display: flex;
    justify-content: space-between;
    &__title{
      font-size: 30px;
      line-height: 56px;
      &--text{
        text-decoration: none;
        color: #fff;
      }
    }
    &__right{
      &--btn{
        text-decoration: none;
        line-height: 56px;
        color: #fff;
        margin-left:15px;
      }
    }
  }
  .contents{
    margin: 30px auto 0;
    background-color: #fff;
    height: 600px;
    width: 70%;
    overflow: scroll;
    .content{
      padding: 10px;
      border-bottom: 1px solid #f6f6f6;
      display: flex;
      &__left{
        padding-right: 20px;
        &--image{
          height: 55px;
          width: 55px;
          border: solid 1px;
        }
      }
      &__right{
        width: calc(100%-100px);
        &__top{
          font-size: 20px;
          font-weight: bold;
          &--title{
            text-decoration: none;
            color: #000000;
          }
        }
        &__bottom{
          font-size: 15px;
          color: #999;
          display: flex;
          &--userName{
            &--btn{
              text-decoration: none;
              color: #999;
            }
          }
          &--date{
            margin-left: 20px;
          }
        }
      }
      &__title{
        font-size: 30px;
        font-weight: bold;
        padding-top: 30px;
        text-align: center;
      }
    }
    .textTitle{
      padding-top: 20px;
      text-align: center;
      font-size: 20px;
      font-weight: bold;
    }
    .textDate{
      font-size: 15px;
      color: #999;
      text-align: center;
    }
    .textAuthor{
      font-size: 15px;
      color: #999;
      text-align: center;
    }
    .textManage{
      display: flex;
      justify-content: center;
      margin-top: 10px;
      &__edit{
        display: block;
        border-radius: 5px;
        color: #ffffff;
        background-color:#66FFCC;
        padding: 0 10px;
        text-decoration: none;
        margin-right: 10px;
      }
      &__delete{
        display: block;
        border-radius: 5px;
        color: #ffffff;
        background-color:#66FFCC;
        padding: 0 10px;
        text-decoration: none;
        margin-left: 10px;
      }
    }
    .textText{
      margin: 20px 30px;
    }
  }
  .form{
    width: 85%;
    height: 100%;
    margin: 0 auto;
    padding-top: 30px;
    &__title{
      display: block;
      width: 100%;
      height: 50px;
    }
    &__text{
      display: block;
      width: 100%;
      height: 50%;
      margin-top: 15px;
    }
    &__btn{
      display: block;
      width: 30%;
      margin: 15px auto 0;
    }
  }
  .user{
    padding: 15px 30px;
  }
}

リセットCSSを配置

リセットCSSを下記のように記述し、ブラウザで表示内容を確認する。
app/assets/stylesheets/reset.scss

/*! normalize.css v2.1.3 | MIT License | git.io/normalize */
/* ==========================================================================
 * HTML5 display definitions
 * ========================================================================== */
/*
 * Correct `block` display not defined in IE 8/9. */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
  display: block;
}

/*
 * Correct `inline-block` display not defined in IE 8/9. */
audio,
canvas,
video {
  display: inline-block;
}

/*
 * Prevent modern browsers from displaying `audio` without controls.
 * Remove excess height in iOS 5 devices. */
audio:not([controls]) {
  display: none;
  height: 0;
}

/*
 * Address `[hidden]` styling not present in IE 8/9.
 * Hide the `template` element in IE, Safari, and Firefox < 22. */
[hidden],
template {
  display: none;
}

/* ==========================================================================
 * Base
 * ========================================================================== */
/*
 * 1. Set default font family to sans-serif.
 * 2. Prevent iOS text size adjust after orientation change, without disabling
 *    user zoom. */
html {
  font-family: sans-serif;
  /* 1 */
  -ms-text-size-adjust: 100%;
  /* 2 */
  -webkit-text-size-adjust: 100%;
  /* 2 */
}

/*
 * Remove default margin. */
body {
  margin: 0;
}

/* ==========================================================================
 * Links
 * ========================================================================== */
/*
 * Remove the gray background color from active links in IE 10. */
a {
  background: transparent;
}

/*
 * Address `outline` inconsistency between Chrome and other browsers. */
a:focus {
  outline: thin dotted;
}

/*
 * Improve readability when focused and also mouse hovered in all browsers. */
a:active,
a:hover {
  outline: 0;
}

/* ==========================================================================
 * Typography
 * ========================================================================== */
/*
 * Address variable `h1` font-size and margin within `section` and `article`
 * contexts in Firefox 4+, Safari 5, and Chrome. */
h1 {
  font-size: 2em;
  margin: 0.67em 0;
}

/*
 * Address styling not present in IE 8/9, Safari 5, and Chrome. */
abbr[title] {
  border-bottom: 1px dotted;
}

/*
 * Address style set to `bolder` in Firefox 4+, Safari 5, and Chrome. */
b,
strong {
  font-weight: bold;
}

/*
 * Address styling not present in Safari 5 and Chrome. */
dfn {
  font-style: italic;
}

/*
 * Address differences between Firefox and other browsers. */
hr {
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  height: 0;
}

/*
 * Address styling not present in IE 8/9. */
mark {
  background: yellow;
  color: black;
}

/*
 * Correct font family set oddly in Safari 5 and Chrome. */
code,
kbd,
pre,
samp {
  font-family: monospace, serif;
  font-size: 1em;
}

/*
 * Improve readability of pre-formatted text in all browsers. */
pre {
  white-space: pre-wrap;
}

/*
 * Set consistent quote types. */
q {
  quotes: "\201C" "\201D" "\2018" "\2019";
}

/*
 * Address inconsistent and variable font size in all browsers. */
small {
  font-size: 80%;
}

/*
 * Prevent `sub` and `sup` affecting `line-height` in all browsers. */
sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}

sup {
  top: -0.5em;
}

sub {
  bottom: -0.25em;
}

/* ==========================================================================
 * Embedded content
 * ========================================================================== */
/*
 * Remove border when inside `a` element in IE 8/9. */
img {
  border: 0;
}

/*
 * Correct overflow displayed oddly in IE 9. */
svg:not(:root) {
  overflow: hidden;
}

/* ==========================================================================
 * Figures
 * ========================================================================== */
/*
 * Address margin not present in IE 8/9 and Safari 5. */
figure {
  margin: 0;
}

/* ==========================================================================
 * Forms
 * ========================================================================== */
/*
 * Define consistent border, margin, and padding. */
fieldset {
  border: 1px solid silver;
  margin: 0 2px;
  padding: 0.35em 0.625em 0.75em;
}

/*
 * 1. Correct `color` not being inherited in IE 8/9.
 * 2. Remove padding so people aren't caught out if they zero out fieldsets. */
legend {
  border: 0;
  /* 1 */
  padding: 0;
  /* 2 */
}

/*
 * 1. Correct font family not being inherited in all browsers.
 * 2. Correct font size not being inherited in all browsers.
 * 3. Address margins set differently in Firefox 4+, Safari 5, and Chrome. */
button,
input,
select,
textarea {
  font-family: inherit;
  /* 1 */
  font-size: 100%;
  /* 2 */
  margin: 0;
  /* 3 */
}

/*
 * Address Firefox 4+ setting `line-height` on `input` using `!important` in
 * the UA stylesheet. */
button,
input {
  line-height: normal;
}

/*
 * Address inconsistent `text-transform` inheritance for `button` and `select`.
 * All other form control elements do not inherit `text-transform` values.
 * Correct `button` style inheritance in Chrome, Safari 5+, and IE 8+.
 * Correct `select` style inheritance in Firefox 4+ and Opera. */
button,
select {
  text-transform: none;
}

/*
 * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio`
 *    and `video` controls.
 * 2. Correct inability to style clickable `input` types in iOS.
 * 3. Improve usability and consistency of cursor style between image-type
 *    `input` and others. */
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
  -webkit-appearance: button;
  /* 2 */
  cursor: pointer;
  /* 3 */
}

/*
 * Re-set default cursor for disabled elements. */
button[disabled],
html input[disabled] {
  cursor: default;
}

/*
 * 1. Address box sizing set to `content-box` in IE 8/9/10.
 * 2. Remove excess padding in IE 8/9/10. */
input[type="checkbox"],
input[type="radio"] {
  box-sizing: border-box;
  /* 1 */
  padding: 0;
  /* 2 */
}

/*
 * 1. Address `appearance` set to `searchfield` in Safari 5 and Chrome.
 * 2. Address `box-sizing` set to `border-box` in Safari 5 and Chrome
 *    (include `-moz` to future-proof). */
input[type="search"] {
  -webkit-appearance: textfield;
  /* 1 */
  -moz-box-sizing: content-box;
  -webkit-box-sizing: content-box;
  /* 2 */
  box-sizing: content-box;
}

/*
 * Remove inner padding and search cancel button in Safari 5 and Chrome
 * on OS X. */
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}

/*
 * Remove inner padding and border in Firefox 4+. */
button::-moz-focus-inner,
input::-moz-focus-inner {
  border: 0;
  padding: 0;
}

/*
 * 1. Remove default vertical scrollbar in IE 8/9.
 * 2. Improve readability and alignment in all browsers. */
textarea {
  overflow: auto;
  /* 1 */
  vertical-align: top;
  /* 2 */
}

/* ==========================================================================
 * Tables
 * ========================================================================== */
/*
 * Remove most spacing between table cells. */
table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* ==========================================================================
 * CSS SETTING
 * by Hiroaki WAKATSUKI
 * Converted 2014/2/16
 * ========================================================================== */
*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

img,
object,
embed {
  max-width: 100%;
  height: auto;
}

object,
embed {
  height: 100%;
}

.left {
  float: left;
}

.right {
  float: right;
}

.clearfix {
  *zoom: 1;
}
.clearfix:after {
  content: "\0020";
  display: block;
  height: 0;
  clear: both;
  overflow: hidden;
  visibility: hidden;
}

.text-left {
  text-align: left;
}

.text-right {
  text-align: right;
}

.text-center {
  text-align: center;
}

.text-justify {
  text-align: justify;
}

.hide {
  display: none;
}

.antialiased {
  -webkit-font-smoothing: antialiased;
}

img {
  display: inline-block;
  vertical-align: middle;
}

textarea {
  height: auto;
  min-height: 50px;
}

select {
  width: 100%;
}

i {
  font-style: normal;
}

.animate {
  -moz-transition-property: all;
  -o-transition-property: all;
  -webkit-transition-property: all;
  transition-property: all;
  -moz-transition-duration: 0.2s;
  -o-transition-duration: 0.2s;
  -webkit-transition-duration: 0.2s;
  transition-duration: 0.2s;
  -moz-transition-timing-function: ease-in;
  -o-transition-timing-function: ease-in;
  -webkit-transition-timing-function: ease-in;
  transition-timing-function: ease-in;
}

.button {
  background-image: -moz-linear-gradient(top, rgba(255, 255, 255, 0.05) 1%, rgba(0, 0, 0, 0.2) 100%);
  background-image: -webkit-linear-gradient(top, rgba(255, 255, 255, 0.05) 1%, rgba(0, 0, 0, 0.2) 100%);
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.05) 1%, rgba(0, 0, 0, 0.2) 100%);
}

ul {
  margin: 0;
  padding: 0;
}
ul li {
  list-style: none;
  padding: 0;
  margin: 0;
}

.row {
  width: 100%;
  margin-left: auto;
  margin-right: auto;
  margin-top: 0;
  margin-bottom: 0;
  max-width: 660px;
  *zoom: 1;
}
.row .grid-1,
.row .grid-2,
.row .grid-3,
.row .grid-4,
.row .grid-5,
.row .grid-6,
.row .grid-7,
.row .grid-8,
.row .grid-9,
.row .grid-10,
.row .grid-11,
.row .grid-12 {
  float: left;
  margin-left: 0.9%;
  margin-right: 0.9%;
}
.row .grid-1 {
  width: 6.53333%;
}
.row .grid-2 {
  width: 14.86667%;
}
.row .grid-3 {
  width: 23.2%;
}
.row .grid-4 {
  width: 31.53333%;
}
.row .grid-5 {
  width: 39.86667%;
}
.row .grid-6 {
  width: 48.2%;
}
.row .grid-7 {
  width: 56.53333%;
}
.row .grid-8 {
  width: 64.86667%;
}
.row .grid-9 {
  width: 73.2%;
}
.row .grid-10 {
  width: 81.53333%;
}
.row .grid-11 {
  width: 89.86667%;
}
.row .grid-12 {
  width: 98.2%;
}

.row:before, .row:after {
  content: " ";
  display: table;
}

.row:after {
  clear: both;
}

.row.collapse > .column,
.row.collapse > .columns {
  position: relative;
  padding-left: 0;
  padding-right: 0;
  float: left;
}

.row.collapse .row {
  margin-left: 0;
  margin-right: 0;
}

.row .row {
  width: auto;
  margin-left: -0.9375em;
  margin-right: -0.9375em;
  margin-top: 0;
  margin-bottom: 0;
  max-width: none;
  *zoom: 1;
}

.row .row:before, .row .row:after {
  content: " ";
  display: table;
}

.row .row:after {
  clear: both;
}

.row .row.collapse {
  width: auto;
  margin: 0;
  max-width: none;
  *zoom: 1;
}

.row .row.collapse:before, .row .row.collapse:after {
  content: " ";
  display: table;
}

.row .row.collapse:after {
  clear: both;
}

@media only screen and (max-width: 40em) {
  .row {
    width: 100%;
    padding: 0 20px !important;
  }
  .row .grid-1,
  .row .grid-2,
  .row .grid-3,
  .row .grid-4,
  .row .grid-5,
  .row .grid-6,
  .row .grid-7,
  .row .grid-8,
  .row .grid-9,
  .row .grid-10,
  .row .grid-11,
  .row .grid-12 {
    float: none;
    width: 100% !important;
    margin: 0 !important;
    position: static !important;
  }
}
/* max-width 640px, mobile-only styles, use when QAing mobile issues */
@media only screen and (min-width: 40.063em) and (max-width: 64em) {
  .row .grid-1,
  .row .grid-2,
  .row .grid-3,
  .row .grid-4 {
    width: 48.2% !important;
  }
}
/* min-width 641px and max-width 1024px, use when QAing tablet-only issues */

下図のように表示されていれば第2章は成功です。

f:id:erwinmarvin:20200915221534p:plain
トップ画面

【第1章】オンライン教科書サービス作成【新しいアプリの立ち上げ】

目的

紙媒体のテキストを用いて行われている学校教育や社会人の社内外研修、資格取得の講義などをオンラインへ移行し、紙媒体から脱却する。 スマートデバイス(PC、スマートフォンタブレット)さえあれば、小中高大学の5教科のテキスト、社内外研修テキストなどあらゆる教科書をいつでも閲覧できるようにするアプリケーションを開発する。 当該章では上記機能を備えたアプリケーションの雛形を作成する。

新しいアプリケーションの立ち上げ

新しくアプリケーションを立ち上げる時には、rails newコマンドを実行する。
ターミナル

rails _5.2.3_ new "online_text" -d mysql
cd online_text
bundle install

bundle install 実行後、下記内容が表示されていれば正常に雛形作成ができている。
ターミナル表示

Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

データベース作成

アプリケーションに紐づくデータベースを作成する時には、rails db:createコマンドを実行する。
ターミナル

cd online_text
rails db:create

rails db:create実行後、下記内容が表示されていれば正常にデータベースが生成されている。
ターミナル表示

Created database 'online_text_development'
Created database 'online_text_test'

サーバーを立ち上げてアプリケーションの雛形が作成されているか確認

正常にアプリケーションが作成できているか、サーバーをrails sコマンドで立ち上げて確認する

cd online_text
rails s

rails s実行後、下記URLへアクセスし、下画面が表示されていれば雛形作成ができている。
URLhttp://localhost:3000/

f:id:erwinmarvin:20200914204539j:plain
新規アプリケーション作成成功時画面

gitへの登録

GitHub Desktopを用いてコミットしプッシュする。
GitHub Desktopの導入は別記事に記載する。
githubを用いて開発するメリットについては別記事に記載する。
当該アプリケーション開発は細かくgithubにコミットし、開発のいつ時点でエラー等が発生しているかなどトレースができる状態を保持することとする。

【Ruby/学習備忘録】オブジェクト指向とは何か

オブジェクト指向とは何か

オブジェクトとは・・・データと処理の集合  例:人物をオブジェクトとした場合
  データ:姓名(name)、性別(gender)、生年月日(birthday)、出身地(birthplace)、職業(occupation)
  処理:自己紹介(introduction_myself)

class Human
  def initialize(name, gender, birthday, birthplace, occupation)
    @name = name
    @gender = gender
    @birthday = birthday
    @birthplace = birthplace
    @occupation = occupation
  end

  def introduction_myself
    puts "私の名前は" + @name + "です。 " + 
    "性別は"    + @gender    + "です。 "+ 
    "生年月日は" + @birthday  + "です。 "+ 
    "出身地は"  + @birthplace + "です。 "+ 
    "職業は"    + @occupation + "です。 "
  end
end

shimizu = Human.new("shimizu", "", "1991/3/20", "岐阜県", "研究職")
shimizu.introduction_myself
用語説明

 Human
  クラス
  具体的なデータに関連付かないデータの抽象的な挙動を定義するもの
 shimizu
  Humanクラスの インスタンス
  クラスから生成される具体的なデータと処理をまとめたもの
 shimizu.introduction_myself
  インスタンスメソッド。
  インスタンスがもつデータを用いてデータを処理したもの

オブジェクト指向の目的

 オブジェクト指向はユーザーのためではなく、開発者のコミュニケーションを円滑にするためのもの
  ×ユーザー
  ○開発者
 オブジェクト指向は手続き型に比べコード行数が多くなる傾向にあるが、プログラムに機能追加が増えてくると、
 各メソッドが分離されているオブジェクト型の方が可読性がよくなる。
 上記のように「自己紹介する」というメソッドをHumanクラスの中に入れてしまうことで、仕様変更等に伴う手間を削減することができる。
 オブジェクト指向を用いて得られるメリットの中の1つが変更に強いこと。
 現在存在するアプリやサービスはユーザーにとっては、独創的なものに見えるが、今までのソフトウェア開発の歴史なかで真に新しい概念を用いて開発されるものはほとんどない。
 ほぼ今までに作られた仕組みを部分的に再利用して開発されている。
 オブジェクト指向でサービス開発する場合、将来的に追加されるであろう機能を予測してコーディングしておくことが求められる。
 システムエンジニアの設計力や予測力が重要になってくる。
 良いエンジニアとそうでないエンジニアを分けることができるのが想像力である。

【サービス開発/1日目】紙媒体の教科書のオンラインテキスト化

サービス概要

 小・中・高・大学、社内外研修、資格講習等で使用されている紙媒体のテキストをオンラインにて作成し、閲覧することができるサービス

メリット

 物理的なテキストの問題からの解放(持ち運ばない, 捨てない)
 テキスト内容の更新がリアルタイムで実施可能(更新のたびに印刷する必要なし)
 研修会場等のテナント代解消
 研修講師代の削減
 入社前教育の充実
 即戦力の育成(中途・新人の即配属が可能なため採用後コスト削減)
 5教科を自分のペースで進められる(先取り思考)
 5教科以外の分野のテキストも閲覧し自分で進められる
 資格試験の地域格差の減少(特殊な試験は開催会場が限定的)

デメリット

 初期作成に時間がかかる

開発動機

 学生時代に学ぶ内容として5教科では現代社会に即していないため、5教科以外の分野のテキストも5教科と同じプラットフォームに掲載し、  興味のある分野を自分自身で学習を進められるようにするため。

 講習会場が限定的な特殊な資格講習をオンライン上でテキストを取得し移動なしで受講することができる。(移動費の抑制)

 入社前教育に活用することで、入社後の教育機関を少なくすることができ、現場に即時配属することが可能となる。(全体的な費用の削減)

ToDo

 サイトマップ作成
 画面遷移図(ワイヤーフレーム
 DB設計書
 サーバー

モックアップ

 開発対象のイメージを共有するためのモックアップを下図に示す。
 例として、下図のように学生が5教科の他の分野も勉強が可能となる。  

f:id:erwinmarvin:20200829214342p:plain
トップ画面

最後に

  まずは最低限の機能だけを備えたサービスをデプロイするところまでを1ヶ月以内に実施する。