目次

ニュース

日記カテゴリ

書庫

RailsでHello Worldを表示させてみましょう。


今回の作業は以下の通りです。
1.Railsのプロジェクトを作成する。
2.controllerを作成する。
3.viewを作成する。
4.WEBrickを起動する。
5.ブラウザを起動する。

 

1.Railsのプロジェクトを作成する。
まずはRailsのプロジェクトを作成します。

プロジェクトを作成したいディレクトリに移動して、
次のコマンドを入力します。

rails HelloProject

 


こんな感じで沢山のファイルやフォルダが作成されます。

 

今回のHello Worldを表示させるために必要なフォルダは次の2つです。
app/controllers         
app/views 


2.controllerを作成する。

Hello Worldの文字列を設定するために、コントローラを次のコマンドで作成します。

ruby script/generate controller HelloWorld


次に作成された「hello_world_controller.rb」にindexというメソッド(アクション)を定義します。

「app/controllers/hello_world_controller.rb」
class HelloWorldController < ApplicationController
  def index
    @message = "Hello World"
  end
end


3.viewを作成する。
次はこの@messageを受け取って表示するために「index.rhtml」というviewを作成します。
「app/views/hello_world/index.rhtml」

ファイルの内容は次の1行だけです。
<h1> <%= @message %> </h1>


4.WEBrickを起動する。
Railsに同梱されているWEBrickを起動して、Hello Worldを表示させてみましょう。
起動するために次のコマンドを入力します。

ruby script/server



5.ブラウザを起動する。

ブラウザを起動し、「http://localhost:3000/hello_world」にアクセスしましょう。
 


無事、「Hello World」と表示されました!

投稿日時 : 2007年7月10日 21:44
Feedback
  • # Howdy! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyhow, I'm definitely glad I found it and I'll be bookmarking and checking back often!
    Howdy! I could have sworn I've been to this websit
    Posted @ 2019/04/10 17:48
    Howdy! I could have sworn I've been to this website before
    but after checking through some of the post I realized it's new to me.
    Anyhow, I'm definitely glad I found it and I'll be bookmarking and checking back
    often!
  • # It's a shame you don't have a donate button! I'd definitely donate to this brilliant blog! I suppose for now i'll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Faceb
    It's a shame you don't have a donate button! I'd d
    Posted @ 2019/05/12 15:37
    It's a shame you don't have a donate button! I'd definitely
    donate to this brilliant blog! I suppose for now i'll settle for
    bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will share this blog with my Facebook group.
    Chat soon!
  • # I'm gone to tell my little brother, that he should also visit this website on regular basis to obtain updated from latest news.
    I'm gone to tell my little brother, that he should
    Posted @ 2019/05/13 2:31
    I'm gone to tell my little brother, that he should also visit this website
    on regular basis to obtain updated from latest news.
  • # Asking questions are genuinely fastidious thing if you are not understanding anything completely, however this article offers good understanding yet.
    Asking questions are genuinely fastidious thing if
    Posted @ 2019/05/15 19:09
    Asking questions are genuinely fastidious thing
    if you are not understanding anything completely, however this
    article offers good understanding yet.
  • # Hi, just wanted to mention, I loved this blog post. It was funny. Keep on posting!
    Hi, just wanted to mention, I loved this blog post
    Posted @ 2019/11/18 18:04
    Hi, just wanted to mention, I loved this blog post.
    It was funny. Keep on posting!
  • # Hi, just wanted to mention, I loved this blog post. It was funny. Keep on posting!
    Hi, just wanted to mention, I loved this blog post
    Posted @ 2019/11/18 18:05
    Hi, just wanted to mention, I loved this blog post.
    It was funny. Keep on posting!
  • # Hi, just wanted to mention, I loved this blog post. It was funny. Keep on posting!
    Hi, just wanted to mention, I loved this blog post
    Posted @ 2019/11/18 18:06
    Hi, just wanted to mention, I loved this blog post.
    It was funny. Keep on posting!
  • # Hi, just wanted to mention, I loved this blog post. It was funny. Keep on posting!
    Hi, just wanted to mention, I loved this blog post
    Posted @ 2019/11/18 18:07
    Hi, just wanted to mention, I loved this blog post.
    It was funny. Keep on posting!
タイトル  
名前  
Url
コメント   

Blog 利用状況