Rails3レシピブックを読みながらRailsを学ぶ
参考
- Rail3レシピブック 190の技
準備
- はじめにbundleでrailsをインストールする……が、なんかエラーが。
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.5), and Bundler cannot continue. Make sure that `gem install json -v '1.6.5'` succeeds before bundling.
bundleするまえにjsonをインストールしてくれと言われた。ので、そのとおりにgem install……。
$ gem install json -v '1.6.5' ERROR: Error installing json: 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'
まだだめか。Development-Kitが必要? Development Kit · oneclick/rubyinstaller Wiki · GitHubへ。
どれ落とせばいいんだろうと思ったら、
for the legacy RubyInstaller v1.8.6 use the DevKit-3.4.5 available at our archive downloads page. For RubyInstaller versions 1.8.7, 1.9.2, and 1.9.3 use the DevKit 4.5.2 from our main downloads page.
という事らしいので4.5.2をDLしexeを実行。さらに以下を実行。
$ cd <DEVKIT_INSTALL_DIR> $ 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が生成されるので例に従ってRubyのパスを記述。
8 # Example: 9 # 10 # --- 11 # - C:/ruby19trunk 12 # - C:/ruby192dev 13 # 14 --- +15 - C:\rubies\Ruby-187-p352
で、インストール。
$ ruby dk.rb review Based upon the settings in the 'config.yml' file generated from running 'ruby dk.rb init' and any of your customizations, DevKit functionality will be injected into the following Rubies when you run 'ruby dk.rb install'. C:/rubies/Ruby-187-p352 $ ruby dk.rb install [INFO] Updating convenience notice gem override for 'C:/rubies/Ruby-187-p352' [INFO] Installing 'C:/rubies/Ruby-187-p352/lib/ruby/site_ruby/devkit.rb'
満を持してbundle install再び。
… Installing json (1.6.5) with native extensions …
オッケー。
アプリケーション作成
- rails new アプリ名で作成
$ bundle exec rails new sampleapp create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app create app/assets/images/rails.png
- rails new アプリ名 -d DBでDBを指定して作成もできる。省略した場合はSQLite3
- このアプリ名は変数Rails.rootで参照できる
- Rails.rootでbundle install(また?)
Bundleで入れたRailsでRailsアプリをnewしてそのRailsアプリでまたBundleしてるのが気持ち悪いけどこんなもんなのかな。
DB設定
これはRedmineでやった事ある。
- config/database.ymlを編集……し……?
6 development: 7 adapter: sqlite3 8 database: db/development.sqlite3 9 pool: 5 10 timeout: 5000
あれ? もうよろしくされてるじゃん。
バージョン等
- rail -vでRailsのバージョン
$ bundle exec rails -v Rails 3.2.1
- rake aboutでRailが使っているライブラリ
$ bundle exec rake about About your application's environment Ruby version 1.8.7 (i386-mingw32) RubyGems version 1.8.12 Rack version 1.4 Rails version 3.2.1 JavaScript Runtime Node.js (V8) Active Record version 3.2.1 …
とりあえず起動
$ bundle exec rails s => Booting WEBrick => Rails 3.2.1 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2012-02-28 19:27:58] INFO WEBrick 1.3.1 [2012-02-28 19:27:58] INFO ruby 1.8.7 (2011-06-30) [i386-mingw32] [2012-02-28 19:27:58] INFO WEBrick::HTTPServer#start: pid=5396 port=3000
- http://localhost:3000にアクセスしてWelcome aboard You’re riding Ruby on Rails! と表示されていれば動いている
Railsアプリ内のファイルとディレクトリ(抜粋)
Redmineでも見たことがあるファイル等もあるが、役割を明確にわかっていなかった。
file/dir | 役割 |
---|---|
config.ru | Rackアプリケーションとして実行する際の設定ファイル |
Rakefile | MakefileのRuby版、MakeのRuby版であるRakeで使う |
app/ | アプリケーションを格納 |
app/assets/ | CoffeeScript等を格納 |
app/controllers/ | コントローラを格納 |
app/models/ | モデルを格納 |
app/views/ | ビューのテンプレート(html,js)を格納 |
app/views/layouts/ | ビューのレイアウトとして使用するhtmlテンプレートを格納 |
app/helper/ | ヘルパーを格納 |
config/ | DB設定、環境設定等各種設定ファイルを格納 |
config/environments/ | 各実行環境用の設定ファイルを格納 |
config/initializers/ | 初期化時に実行されるファイルを格納 |
config/locales/ | I18n用のファイルを格納 |
db/ | DB操作に使用するスクリプトを格納、SQLite3の場合はDBファイルも |
db/migrate/ | DBスキーマの編集のためのマイグレーションファイルを格納 |
lib/ | MVCいずれにも属さない処理をライブラリ化し格納 |
script/ | 各種スクリプトを格納 |
public | 静的ファイル(html,js,css,画像)を格納 |
test/ | テストコードを格納 |
vendor/ | サードパーティ製のライブラリ、外部ライブラリを格納 |
vendor/plugins/ | プラグインを格納 |
ほとんどになってしまった……。とりあえず役割は覚えておこう。ただし一部ピンときてない場所もあり。
続く。Rails3レシピブックを読みながらRailsを学ぶ モデル、コントローラ、ビュー、Railsの規約など - kk_Atakaの日記