RailsでHello, World!を表示してみた。

今回はこちらのサイトを参考に、RailsでHello, World!を表示してみました。この記事は実施内容の備忘録となります。
Rails をはじめよう - Rails ガイド

1. 環境

2. プロジェクトの作成

まずはコマンドプロンプトでプロジェクトを作成していきます。

rails new hello_world

f:id:edasaka:20191212122825p:plain
rails new hello_world
f:id:edasaka:20191212123609p:plain
プロジェクト作成完了

3. WEBサーバーの起動

WEBサーバーを起動してアプリケーションが起動することを確認していきます。

cd hello_world
rails server

f:id:edasaka:20191212123801p:plain
rails server
f:id:edasaka:20191212123851p:plain
アプリケーションが起動しました。

4. コントローラーとビューの作成

Hello, World!用のコントローラーとビューを作成していきます。

rails generate controller helloworld index

f:id:edasaka:20191212124223p:plain
rails generate controller

5. ビューの編集

app/views/welcome/index.html.erbを編集して「Hello,World!」を表示するようにします。

<h1>Hello,World!</h1>

f:id:edasaka:20191212130553p:plain
index.html.erb

localhost:3000/helloworld/index を開くと、Hello,Worldが表示できました!

f:id:edasaka:20191212125324p:plain
Hello,Wolrd

6. ルーティングの設定

localhost:3000にアクセスした際に、Hello,Worldを表示できるよう、config/routes.rbに下記を追加します。

root 'helloworld#index'

f:id:edasaka:20191212125753p:plain
ルーティングの設定

localhost:3000にアクセスすると、Hello,Worldが表示できました!

f:id:edasaka:20191212125855p:plain
localhost:3000


以上、RailsでHello, World!を表示してみた。でした。