RailsOffice / 080930


RailsOffice

RubyOnRails?勉強会"第1回"

  • Railsを使ってWebアプリケーション作成

Rails2のインストール

  • FreeBSDの場合,portsで簡単インストール
    sudo -s
    cd /usr/ports/www/rubygem-rails/
    make install
    make clean

Railsのアップデート

gem update rails

helloworldを表示しよう

プロジェクト(作業用ディレクトリ)の作成

%rails helloworld

ディレクトリ(helloworld)と,ファイル群が作成される

%cd helloworld

hello worldスクリプト群の作成

script/generate controller helloworld
#script/generate: Command not found.が表示される
cd app
emacs controllers/helloworld_controller.rb

以下,appディレクトリ下で作業。まず,コントローラを作成。

# 編集前のcontrollers/helloworld_controller.rb
# しかし、自分のファイルはこのような内容でなく、何も表示されなかった
class HelloController < ApplicationController
end
# 編集後のcontrollers/helloworld_controller.rb
class HelloworldController < ApplicationController
  def index
    @message = "hogehoge"
  end
end

HTML表示用viewを作成

emacs views/helloworld/index.rhtml

views/helloworld/index.rhtmlの中身

<HTML>
<head>
  <title>hello world</title>
</head>
<body>
  <h1>Hello world (rails) project</h1>
  <%=  @message   %> <!-- koko ga Ruby on Rails -->
</body>
</HTML>

起動

ポート番号を指定して起動。以下の例では6000番ポートを指定

script/server -p 6000

ブラウザでアクセス

http://your.host.name:6000/helloworld/index/