Rails3事始め

rails3のインストール

$ gem install rails
Successfully installed activesupport-3.0.0
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.1
Successfully installed activemodel-3.0.0
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.5
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.0
Successfully installed arel-1.0.1
Successfully installed activerecord-3.0.0
Successfully installed activeresource-3.0.0
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.8
Successfully installed mail-2.2.6.1
Successfully installed actionmailer-3.0.0
Successfully installed rake-0.8.7
Successfully installed thor-0.14.1
Successfully installed railties-3.0.0
Successfully installed bundler-1.0.0
Successfully installed rails-3.0.0
24 gems installed

ウホッ。一杯入るのね。

railsプロジェクト作成

$ rails new huddle
$ cd huddle

見慣れないファイルがあるので、しばし鑑賞。

.gitignore
Gemfile
README
Rakefile
app/
config/
config.ru
db/
doc/
lib/
log/
public/
script/
test/
tmp/
vendor/


これが噂のbundle関連のファイルか。

$ cat Gemfile
source 'http://rubygems.org'

gem 'rails', '3.0.0'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug'

# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
#   gem 'webrat'
# end
$ cat config.ru 
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment',  __FILE__)
run Huddle::Application


最初からgitignoreがある。素晴らしい!

$ cat .gitignore 
.bundle
db/*.sqlite3
log/*.log
tmp/**/*

続き

$ rake db:create:all
(in /Users/rochefort/work/rails3/huddle)
Could not find gem 'sqlite3-ruby (>= 0, runtime)' in any of the gem sources.
Try running `bundle install`.

sqlite3-rubyが無いよと怒られる。
gemで入れるかと思ったが、メッセージの最後に「bundle install」と。

$ bundle install
Fetching source index for http://rubygems.org/
Using rake (0.8.7) 
Using abstract (1.0.0) 
Using activesupport (3.0.0) 
Using builder (2.1.2) 
Using i18n (0.4.1) 
Using activemodel (3.0.0) 
Using erubis (2.6.6) 
Using rack (1.2.1) 
Using rack-mount (0.6.13) 
Using rack-test (0.5.5) 
Using tzinfo (0.3.23) 
Using actionpack (3.0.0) 
Using mime-types (1.16) 
Using polyglot (0.3.1) 
Using treetop (1.4.8) 
Using mail (2.2.6.1) 
Using actionmailer (3.0.0) 
Using arel (1.0.1) 
Using activerecord (3.0.0) 
Using activeresource (3.0.0) 
Using bundler (1.0.0) 
Using thor (0.14.1) 
Using railties (3.0.0) 
Using rails (3.0.0) 
Installing sqlite3-ruby (1.3.1) with native extensions 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Your bundle was installed to `/Users/rochefort/.rvm/gems/ruby-1.9.2-p0`


$ bundle show
Gems included by the bundle:
  * abstract (1.0.0)
  * actionmailer (3.0.0)
  * actionpack (3.0.0)
  * activemodel (3.0.0)
  * activerecord (3.0.0)
  * activeresource (3.0.0)
  * activesupport (3.0.0)
  * arel (1.0.1)
  * builder (2.1.2)
  * bundler (1.0.0)
  * erubis (2.6.6)
  * i18n (0.4.1)
  * mail (2.2.6.1)
  * mime-types (1.16)
  * polyglot (0.3.1)
  * rack (1.2.1)
  * rack-mount (0.6.13)
  * rack-test (0.5.5)
  * rails (3.0.0)
  * railties (3.0.0)
  * rake (0.8.7)
  * sqlite3-ruby (1.3.1)
  * thor (0.14.1)
  * treetop (1.4.8)
  * tzinfo (0.3.23)

bundle installするとGemfile.lockが作られた。

$ cat Gemfile.lock 
GEM
  remote: http://rubygems.org/
  specs:
    abstract (1.0.0)
    actionmailer (3.0.0)
      actionpack (= 3.0.0)
      mail (~> 2.2.5)
    actionpack (3.0.0)
      activemodel (= 3.0.0)
      activesupport (= 3.0.0)
      builder (~> 2.1.2)
      erubis (~> 2.6.6)
      i18n (~> 0.4.1)
      rack (~> 1.2.1)
      rack-mount (~> 0.6.12)
      rack-test (~> 0.5.4)
      tzinfo (~> 0.3.23)
    activemodel (3.0.0)
      activesupport (= 3.0.0)
      builder (~> 2.1.2)
      i18n (~> 0.4.1)
    activerecord (3.0.0)
      activemodel (= 3.0.0)
      activesupport (= 3.0.0)
      arel (~> 1.0.0)
      tzinfo (~> 0.3.23)
    activeresource (3.0.0)
      activemodel (= 3.0.0)
      activesupport (= 3.0.0)
    activesupport (3.0.0)
    arel (1.0.1)
      activesupport (~> 3.0.0)
    builder (2.1.2)
    erubis (2.6.6)
      abstract (>= 1.0.0)
    i18n (0.4.1)
    mail (2.2.6.1)
      activesupport (>= 2.3.6)
      mime-types
      treetop (>= 1.4.5)
    mime-types (1.16)
    polyglot (0.3.1)
    rack (1.2.1)
    rack-mount (0.6.13)
      rack (>= 1.0.0)
    rack-test (0.5.5)
      rack (>= 1.0)
    rails (3.0.0)
      actionmailer (= 3.0.0)
      actionpack (= 3.0.0)
      activerecord (= 3.0.0)
      activeresource (= 3.0.0)
      activesupport (= 3.0.0)
      bundler (~> 1.0.0)
      railties (= 3.0.0)
    railties (3.0.0)
      actionpack (= 3.0.0)
      activesupport (= 3.0.0)
      rake (>= 0.8.4)
      thor (~> 0.14.0)
    rake (0.8.7)
    sqlite3-ruby (1.3.1)
    thor (0.14.1)
    treetop (1.4.8)
      polyglot (>= 0.3.1)
    tzinfo (0.3.23)

PLATFORMS
  ruby

DEPENDENCIES
  rails (= 3.0.0)
  sqlite3-ruby

無事実行できた(既にあるから不要だったか)。

rake db:create:all
(in /Users/rochefort/work/rails3/huddle)
db/development.sqlite3 already exists
db/test.sqlite3 already exists
db/production.sqlite3 already exists

おまけ

gitへcommit。
.DS_Storeを追加。db下はseed.rbがあるので.gitignoreは未作成。
tmp下のディレクトリも管理したいのでforceオプションでadd。
(このやり方でいいのか少し不安)

$ cat .gitignore 
.DS_Store
.bundle
db/*.sqlite3
log/*.log
tmp/**/*

$ touch log/.gitignore
$ touch tmp/.gitignore
$ touch tmp/cache/.gitignore tmp/pids/.gitignore tmp/sessions/.gitignore tmp/sockets/.gitignore

$ find . -name .gitignore
./.gitignore
./log/.gitignore
./tmp/.gitignore
./tmp/cache/.gitignore
./tmp/pids/.gitignore
./tmp/sessions/.gitignore
./tmp/sockets/.gitignore

$ git init
$ git add .
$ git add -f tmp/cache/.gitignore tmp/pids/.gitignore tmp/sessions/.gitignore tmp/sockets/.gitignore
$ git commit -m 'initial'

実行

script/serverは無くなったので、script/rails s で起動。
見慣れた画面が現れました。

$ script/rails s
=> Booting WEBrick
=> Rails 3.0.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-09-23 17:26:19] INFO  WEBrick 1.3.1
[2010-09-23 17:26:19] INFO  ruby 1.9.2 (2010-08-18) [x86_64-darwin10.4.0]
[2010-09-23 17:26:19] INFO  WEBrick::HTTPServer#start: pid=5069 port=3000
$ script/rails  --help
Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short-cut alias: "g")
 console     Start the Rails console (short-cut alias: "c")
 server      Start the Rails server (short-cut alias: "s")
 dbconsole   Start a console for the database specified in config/database.yml
             (short-cut alias: "db")
 new         Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

In addition to those, there are:
 application  Generate the Rails application code
 destroy      Undo code generated with "generate"
 benchmarker  See how fast a piece of code runs
 profiler     Get profile information from a piece of code
 plugin       Install a plugin
 runner       Run a piece of code in the application environment

All commands can be run with -h for more information.


さっきまで気付かなかったんですが、localhost:3000をブクマしてる人がいたなんて。
私も記念ブクマしておきました。

あとで

bundlerはもう少し調べよう。