ページ

2011年8月24日水曜日

Rails アプリケーション作成 ~続き (Rails3 on Ubuntu10.04LTS)

○ 以前の記事(2011.03.19、 2011.06.19) の続き 〜 on Ubuntu 10.04 LTS


● Ruby, Railsをインストール
   以前の記事の手順通りに再度インストールから始め、サーバーの起動を確認するところまで出来た
   rvmのインストールで、'rvm notes'を実行すると以下のような表示が。そして気になるのが下線の部分

$ rvm notes

Notes for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.2 LTS" )

NOTE: 'ruby' represents Matz's Ruby Interpreter (MRI) (1.8.X, 1.9.X)
This is the *original* / standard Ruby Language Interpreter
'ree' represents Ruby Enterprise Edition
'rbx' represents Rubinius

bash >= 3.2 is required
curl is required
git is required (>= 1.7 recommended)
patch is required (for ree and some ruby-head's).

If you wish to install rbx and/or Ruby 1.9 head (MRI) (eg. 1.9.2-head),
then you must install and use rvm 1.8.7 first.

If you wish to have the 'pretty colors' again,
set 'export rvm_pretty_print_flag=1' in ~/.rvmrc.

dependencies:
# For RVM
rvm: bash curl git

# For Ruby (MRI, Rubinius, & REE) you should install the following OS dependencies:
ruby: /usr/bin/apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake

# For JRuby (if you wish to use it) you will need:
jruby: /usr/bin/apt-get install curl g++ openjdk-6-jre-headless
jruby-head: /usr/bin/apt-get install ant openjdk-6-jdk

# In addition to ruby: dependencies,
ruby-head: subversion

# For IronRuby (if you wish to use it) you will need:
ironruby: /usr/bin/apt-get install curl mono-2.0-devel

For rbx (Rubinius) more than 600MB of free RAM required.

NOTE: For all installations, as of 1.7, RVM no longer autoloads .rvmrc files. In order to return this functionality, you MUST add 'export rvm_project_rvmrc=1' to your $HOME/.rvmrc file.
This causes RVM to override 'cd' which, while toggleable even < 1.7, is currently defaulted to 'off'. This knob returns the previous behaviour to active which causes per-project .rvmrc files to be loaded once again

 Example: echo 'export rvm_project_rvmrc=1' >> $HOME/.rvmrc && rvm reload


● 各バージョンを確認してみる
$ ruby -v
   ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
$ rails -v
   Rails 3.0.10
$ gem -v
   1.3.7
$ sqlite3 -version
   3.6.22
$ rvm -v
   rvm 1.7.2

● Railsアプリケーション作成開始
$ rails new /home/hito/photos

● Scaffold (データベース内のデータの登録、読込み、更新、削除のためのコードを生成する)
$ cd photos
rails g scaffold photo key:integer alb_tt:text place:text content:text year1:integer year2:string month:integer day:integer filename:text image:text cd_no:string dvd_no:string region:string remark:text people:text

      invoke  active_record
      create    db/migrate/20110821111257_create_photos.rb
      create    app/models/photo.rb
      invoke    test_unit
      create      test/unit/photo_test.rb
      create      test/fixtures/photos.yml
       route  resources :photos
      invoke  scaffold_controller
      create    app/controllers/photos_controller.rb
      invoke    erb
      create      app/views/photos
      create      app/views/photos/index.html.erb
      create      app/views/photos/edit.html.erb
      create      app/views/photos/show.html.erb
      create      app/views/photos/new.html.erb
      create      app/views/photos/_form.html.erb
      invoke    test_unit
      create      test/functional/photos_controller_test.rb
      invoke    helper
      create      app/helpers/photos_helper.rb
      invoke      test_unit
      create        test/unit/helpers/photos_helper_test.rb
      invoke  stylesheets
      create    public/stylesheets/scaffold.css

● ブラウザでアクセスしてみる
127.0.0.1:3000/photos


● Migrate ~DBテーブルの作成
Rails2と同様、$HOME/User/photos/db/migrate に'201108xxxxxxxx_create_photos.rb'といった、DBのフィールドを作成用する要素が生成されている。ファイルの中身が以下

class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table :photos do |t|
      t.integer :key
      t.text :alb_tt
      t.text :place
      t.text :content
      t.integer :year1
      t.string :year2
      t.integer :month
      t.integer :day
      t.text :filename
      t.text :image
      t.string :cd_no
      t.string :dvd_no
      t.string :region
      t.text :remark
      t.text :people

      t.timestamps
    end
  end

Rakeにてデータベースを作成する。以下のコマンドによって、SQLではCREATE TABLEが実行される

$ rake db:migrate

==  CreatePhotos: migrating ===================================================
-- create_table(:photos)
   -> 0.0224s
==  CreatePhotos: migrated (0.0233s) ==========================================

● アプリケーション・ユニットの各種テスト
$ rake

Loaded suite /home/hito/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/rake_test_loader
Started
.
Finished in 0.184379 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 33681
Loaded suite /home/hito/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/rake_test_loader
Started
.......
Finished in 0.755171 seconds.

7 tests, 10 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 56047



● ブラウザに表示する ~コンソールの表示とブラウザの表示
$ rails s
=> Booting WEBrick
=> Rails 3.0.10 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-08-21 20:56:15] INFO  WEBrick 1.3.1
[2011-08-21 20:56:15] INFO  ruby 1.9.2 (2011-07-09) [x86_64-linux]
[2011-08-21 20:56:15] INFO  WEBrick::HTTPServer#start: pid=1734 port=3000
DEPRECATION WARNING: config.action_view.debug_rjs will be removed in 3.1, from 3.1 onwards you will need to install prototype-rails to continue to use RJS templates . (called from service at /home/hito/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111)

Started GET "/photos" for 127.0.0.1 at 2011-08-21 20:56:19 +0900
  Processing by PhotosController#index as HTML
  Photo Load (3.7ms)  SELECT "photos".* FROM "photos"
Rendered photos/index.html.erb within layouts/application (11.6ms)
Completed 200 OK in 149ms (Views: 17.8ms | ActiveRecord: 3.7ms)




この状態で、レコードの新規作成、編集、一覧表示、削除が出来ることを確認 -> Ok


つづく
 
*参照: WEB+DB Press誌 Vol.58 'Rails3'、O'Reilly 'Head First Rails'

0 件のコメント:

コメントを投稿