■ コントローラ作成
■ Wordモデル・Chapterモデル作成
まずはシンプルに作っていこうと思います。
実装したい機能は以下の通り。
* 英単語とその日本語訳を登録することができる
* 登録した英単語は編集、削除もできる
* 30語くらいの単位で章分けする
* 章ごとに小テスト(英単語が表示され、その日本語訳を答える)が作られる
* 小テストが採点される
* 間違えた問題を集めた再テストが作られる
TOP画面を作ります。まだモデルは関連しないのでコントローラを作成。
$ rails g controller Top index
Topコントローラにindexというアクションを作りました。
サーバを起動してhttp://localhost:3000/top/indexにアクセスすると画面が表示されます。
これをトップ画面にしたいので、http://localhost:3000でアクセスできるようにルーティングを変更します
routes.rbにroot 'top#index'
を追加
Rails.application.routes.draw do
root 'top#index'
get 'top/index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
「ルートURLはTopコントローラのindexアクションです」という意味ですね。これで完了です!
TOP画面をどうするかは未定なのですが、とりあえずwelcomeな感じにしておきます。
先ほどの$ rails g
コマンドで作成されたindex.html.erbファイルを編集します。
<h1>*WELCOME*</h1>
<p>take it easy! 0 v 0</p>
wordモデルとchapterモデルを仮で作成します。
主キーのidは自動で追加されるのでここでは明記しません。また、wordモデルのchapter_idも抜いておきます。
$ rails g model Word english:string japanese:string should_retest:boolean
$ rails g model Chapter name:string
これでマイグレーションファイルが2つ作られました。
chapter : word = 1 : n にしたいので、wordの方のマイグレーションファイル20201109_create_words.rbにt.references :chapter, foreign_key: true
を追記します。
class CreateWords < ActiveRecord::Migration[6.0]
def change
create_table :words do |t|
t.string :english
t.string :japanese
t.boolean :should_retest
t.references :chapter, foreign_key: true # ←ここを追記
t.timestamps
end
end
end
DBに反映させます。
$ rails db:migrate
== 20201109093357 CreateWords: migrating ======================================
-- create_table(:words)
-> 0.0140s
== 20201109093357 CreateWords: migrated (0.0141s) =============================
== 20201109093405 CreateChapters: migrating ===================================
-- create_table(:chapters)
-> 0.0029s
== 20201109093405 CreateChapters: migrated (0.0029s) ==========================
コマンドで確認してみます。
$ sqlite3 db/development.sqlite3
sqlite> .tables
ar_internal_metadata schema_migrations
chapters words
chaptersとwordsというテーブルが作成されています。
wordsのカラムを確認すると
sqlite> PRAGMA table_info(words);
cid|name|type|notnull|dflt_value|pk
0|id|integer|1||1
1|english|varchar|0||0
2|japanese|varchar|0||0
3|should_retest|boolean|0||0
4|chapter_id|integer|0||0
5|created_at|datetime(6)|1||0
6|updated_at|datetime(6)|1||0
先ほど主キーのidとchapter_idはマイグレーションファイルに記載していなかったのですが、ちゃんと作成されています。chapter_idはt.references :chapter, foreign_key: trueを追加したおかげで自動作成されたんですね。
モデルができたので、CRUD(Create, Read, Update, Delete)を定義するコントローラを作成します。
命名規則にしたがってwordは複数形に。
$ rails g controller Words new show edit delete
words_controller.rb
class WordsController < ApplicationController
def new
end
def show
end
def edit
end
def delete
end
end
4つ分のhtml.erbも作成されました。もしかしたらdelete.html.erbは要らないかもな
wordコントローラを作成した際にルーティングも↓こんな風に反映されましたが、
routes.rb
Rails.application.routes.draw do
get 'words/new'
get 'words/show'
get 'words/edit'
get 'words/delete'
root 'top#index'
get 'top/index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
resourceメソッドを使えばCRUDのルーティングをまとめて定義することができます。
routes.rb
Rails.application.routes.draw do
root 'top#index'
get 'top/index'
resources :words
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
これで、たとえばnew画面にはhttp://localhost:3000/words/newでアクセスできるようになりました。
本当はCRUDの処理も書きたかったですが時間切れです!
おわり*0 v 0
第1回 | 2020.11.6 * サーバ起動まで |
第2回 | 2020.11.9 * 最初のコントローラとWord・Chapterモデル |
第3回 | 2020.11.10 * 英単語の登録機能 |
第4回 | 2020.11.11 * 削除機能 |
第5回 | 2020.11.12 * 単語の一覧表示 |
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント