Rails

Ruby on Rails

原則

DRY (Don't Repeat Tourself)

同じことを繰り返さないこと

Convention over Configuration

設定よりも決まりごとを優先すること

Railsのインストール

さくらレンタルサーバ(スタンダード以上のプラン)のユーザ領域に入れる。

rubyは最初から入っているのでインストールする必要はない。

さくらのレンタルサーバにRailインストール

環境変数の追加

%vi .cshrc

setenv RUBYLIB $HOME/local/lib
setenv GEM_HOME $HOME/local/lib/ruby/gem
(RUBYLIBに余計なパスを突っ込むと、NameErrorで失敗ので注意)


RubyGemsのインストール

2008/12時点の最新バージョン

% mkdir $HOME/local/src
% cd $HOME/local/src
% wget http://rubyforge.org/frs/download.php/20989/rubygems-1.3.1.tgz
% tar xvzf rubygems-1.3.1.tgz
% cd rubygems-1.3.1
% ruby setup.rb config --prefix=$HOME/local
% rehash
% vi $HOME/.cshrc
「$HOME/lib/ruby/gem/bin」をパスに追加。

set path = ( ... $HOME/local/bin $HOME/local/lib/rubygem/bin)





%gem -v
1.3.1

%gem environment

.cshrcにパス追加

%vi .cshrc

set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin
 /usr/local/bin /usr/X11R6/bin $HOME/bin $HOME/local/bin)

setenv PATH $HOME/local/bin:$HOME/local/lib/ruby/gem/bin:$PATH
setenv PERL5LIB $HOME/local/lib/perl5:$HOME/local/lib/perl5/site_perl
setenv PYTHONPATH $HOME/local/lib/python:$HOME/local/lib/python/site-packages
setenv LD_LIBRARY_PATH $HOME/local/lib
setenv RUBYLIB $HOME/local/lib/ruby/site_ruby/1.8:$HOME/local/lib
setenv GEM_HOME $HOME/local/lib/ruby/gem

GemsでRailsのインストール

%gem18 install rails
Successfully installed rake-0.8.3
Successfully installed activesupport-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed rails-2.2.2
7 gems installed
Installing ri documentation for rake-0.8.3...
Installing ri documentation for activesupport-2.2.2...
Installing ri documentation for activerecord-2.2.2...
Installing ri documentation for actionpack-2.2.2...
Installing ri documentation for actionmailer-2.2.2...
Installing ri documentation for activeresource-2.2.2...
Installing RDoc documentation for rake-0.8.3...
Installing RDoc documentation for activesupport-2.2.2...


railsのパスを通す
%vi .cshrc
set path = ( ...  $HOME/local/lib/ruby/gem/bin)

%source ~/.cshrc

$rails -v
Rails 2.2.2
※たまにサーバの調子が悪いと以下のようなエラーが出る??? rubygems/version.rb が悪いといわれた…。
Bulk updating Gem source index for: http://gems.rubyforge.org

※ rails 実行でエラーが出る場合

$ rails test 
/usr/local/lib/ruby/gems/1.8/gems/rails-2.2.0/bin/../lib/rails_generator/op­tions.rb:32:in 
`default_options': undefined method `write_inheritable_attribute' for 
Rails::Generator::Base:Class (NoMethodError) 
  from 
/usr/local/lib/ruby/gems/1.8/gems/rails-2.2.0/bin/../lib/rails_generator/ba­se.rb:90 
  from 
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in 
`gem_original_require' 
  from 
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in 
`require' 
  from 
/usr/local/lib/ruby/gems/1.8/gems/rails-2.2.0/bin/../lib/rails_generator.rb­:34 
  from 
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in 
`gem_original_require' 
  from 
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in 
`require' 
  from /usr/local/lib/ruby/gems/1.8/gems/rails-2.2.0/bin/rails:13 
  from /usr/local/bin/rails:19:in `load' 
  from /usr/local/bin/rails:19 

railsプロジェクトの作成

%mkdir rails
%cd rails
%rails test
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  config/initializers
      create  config/locales
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  script/process
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application.rb
      create  app/helpers/application_helper.rb
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/environment.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/console
      create  script/dbconsole
      create  script/destroy
      create  script/generate
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  script/performance/request
      create  script/process/reaper
      create  script/process/spawner
      create  script/process/inspector
      create  script/runner
      create  script/server
      create  script/plugin
      create  public/dispatch.rb
      create  public/dispatch.cgi
      create  public/dispatch.fcgi
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log


コントローラー・ビューの生成

%cd test
%ruby script\generate controller hello hi bye
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/hello
      exists  test/functional/
      create  app/controllers/hello_controller.rb
      create  test/functional/hello_controller_test.rb
      create  app/helpers/hello_helper.rb
      create  app/views/hello/hi.html.erb
      create  app/views/hello/bye.html.erb

Railsのアップデート

gem update rails

Rubygemsのアップデート

gem update --system