人気ブログランキング | 話題のタグを見る

RiceBall融解

teramin.exblog.jp
ブログトップ
2012年 04月 05日

Ruby on Rails 3.2.3 アプリケーション開発(準備編)

アプリケーションを開発するための準備、というか枠組みの作成方法について。




まずはアプリケーション開発プロジェクト用ディレクトリを作成する。
今回は「C:\Ruby193\projects」にした。(Rubyインストール時[リンク]のお試しプログラムの置き場所)

アプリケーションの作成

>rails new アプリケーション名
ex) rails new sample002


色んなファイルの作成と「bundle install」とやらが実行される。
「Errno::EHOSTUNREACH」みたいなエラーが出る場合は、Rubyインストール時と同様Proxyの問題なので、コマンドプロンプトで以下を実行。

>set http_proxy=http://[proxyのアドレス]:[proxyのポート番号]
ex) set http_proxy=http://192.168.0.1:80

Proxy設定後、作ったアプリケーションをディレクトリごとごっそり削除して、再度アプリケーションの作成コマンドを実行。

またエラーが発生。

Gem::InstallError: The 'json' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit from 'http://rubyinstaller.org/downloads' and follow the instructions at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit' An error occured while installing json (1.6.6), and Bundler cannot continue. Make sure that `gem install json -v '1.6.6'` succeeds before bundling.


「Development-Kit」とやらをインストールしろ、という話なので、エラーメッセージの中にあるURLにアクセス。
http://rubyinstaller.org/downloads

ページ内の「DEVELOPMENT KIT」セクションにある実行ファイルをダウンロードする。
(この時は「DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe」)

ダウンロードしたファイルを実行すると、解凍先を選択させられる。適当にディレクトリを指定し、解凍。
親フォルダは作成されないので注意。
今回は「C:\Ruby193\devkit_installer」にした。

コマンドプロンプトで上記の展開したディレクトリに移動し、DevKitの初期化を実行する。

>ruby dk.rb init


以下のメッセージが出れば成功。

Initialization complete! Please review and modify the auto-generated 'config.yml' file to ensure it contains the root directories to all of the installed Rubies you want enhanced by the DevKit.

自動生成された「config.yml」を編集しろ、と言われたので開いてみる。

# This configuration file contains the absolute path locations of all
# installed Rubies to be enhanced to work with the DevKit. This config
# file is generated by the 'ruby dk.rb init' step and may be modified
# before running the 'ruby dk.rb install' step. To include any installed
# Rubies that were not automagically discovered, simply add a line below
# the triple hyphens with the absolute path to the Ruby root directory.

Rubyのルートディレクトリのパスを書け、という指令が出たので、最下行に追加する。
僕の環境「C:\Ruby193」がルートディレクトリなので、以下のように追加。

# Example:
#
# ---
# - C:/ruby19trunk
# - C:/ruby192dev
#
---
- C:\Ruby193 (←追加)


「config.yml」を編集後、DevKitのインストールを実行。

>ruby dk.rb install


DevKitのインストールが完了したら、再度アプリケーションの作成を。

アプリケーション開発プロジェクト用ディレクトリに移動し、作成に失敗したアプリケーションをディレクトリごとごっそり削除して、アプリケーション作成コマンドを実行。

Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gemis installed.

作成完了。

お次は実行テスト。WebrickというWebサーバ上で実行させるので、まずはWebrickサーバを起動させる。作成したアプリケーションルートにカレントを移動し、以下を実行。

>rails server

以下のようなメッセージが出ればOK。

=> Booting WEBrick
=> Rails 3.2.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-04-04 15:14:51] INFO WEBrick 1.3.1
[2012-04-04 15:14:51] INFO ruby 1.9.3 (2012-02-16) [i386-mingw32]
[2012-04-04 15:14:51] INFO WEBrick::HTTPServer#start: pid=6752 port=3000

ブラウザから「http://localhost:3000/」にアクセスしてみる。
「Welcome aboard」というページが表示される。
次にアプリケーションの情報を見るために、「About your application’s environment」のリンクをクリック。
アプリケーションルートが「Application root」の項目に表示されてたりするのが分かる。
また、コマンドプロンプトでは、Webrickサーバのアクセスログが表示される。

Webrickサーバの停止は「Ctrl-C」で。

by teramin | 2012-04-05 11:00 | IT/Web関連


<< 香川旅行 うどん まとめ (+...      Ruby on Rails 3... >>