¥È¥Ã¥×   ÊÔ½¸ Åà·ë²ò½ü º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

Ruby/Ruby on Rails/step by step

Last-modified: 2018-11-14 (¿å) 15:24:01 (2186d)
Top / Ruby / Ruby on Rails / step by step

Ruby/Ruby on Rails

Ruby on Rails¤Ç·«¤êÊÖ¤·Áàºî¤·¤Æ³Ð¤¨¤¿¤¤»öÊÁ¤òstep¤È¤·¤ÆÀ°Íý¤·¤Þ¤·¤¿¡£

step01 InstantRails¤Îµ¯Æ°¤Èirb

C:\InstantRails\InstantRails.exe¤ò¥À¥Ö¥ë¥¯¥ê¥Ã¥¯¤·¤Æµ¯Æ°¤·¤Þ¤¹¡£

step01_InstantRails.gif


DOS¥×¥í¥ó¥×¥È²èÌ̤ò³«¤­¡¢irb¤ÇÁàºî¤·¤Æ¤ß¤Þ¤¹¡£
[I¤Î¥¢¥¤¥³¥ó] - [Rails Applications] - [Open Ruby Console Window]

step01_irb.gif

³Æ¥³¥Þ¥ó¥É¤Î°ÕÌ£

¥³¥Þ¥ó¥ÉÆ°ºî
cmd /f:octrl+f¤Ç¤Î¥Õ¥¡¥¤¥ë̾Êä´°¤òÍ­¸ú¤È¤·¤Þ¤¹
ruby -vruby¤Î¥Ð¡¼¥¸¥ç¥ó¤òɽ¼¨¤·¤Þ¤¹
irb -ribr/completionÊä´°µ¡Ç½Í­¤ê¤Çirb¤òµ¯Æ°¤·¤Þ¤¹
pu[tab]ÅÓÃæ¤Çtab¥­¡¼¤ò²¡¤¹¤ÈÊä´°¤¬¸ú¤­¤Þ¤¹
puts "Hello World!"rubyʸ¤òÂǤĤȤ¹¤°¤Ë¼Â¹Ô¤µ¤ì¤Þ¤¹
exitirb¤ò½ªÎ»¤·¤Þ¤¹

¤è¤¯»È¤¦Áàºî¤òrails_appsľ²¼¤Î¥Ð¥Ã¥Á¥Õ¥¡¥¤¥ë¤Ë¤·¤Þ¤·¤¿¡£

¥Ð¥Ã¥Á¥Õ¥¡¥¤¥ë̾ÆâÍÆ
setenv.batcmd /f:o
irb.batC:\InstantRails\ruby\bin\irb -rirb/completion

step02 ¥×¥í¥¸¥§¥¯¥È¤Î¿·µ¬ºîÀ®

  1. InstantRails¤«¤éDOS¥×¥í¥ó¥×¥È¤ò³«¤¯
    ¥«¥ì¥ó¥È¥Õ¥©¥ë¥À¤ÏC:\InstantRails\rails_apps¤È¤Ê¤ê¤Þ¤¹¡£
  2. ¡Örails ¥×¥í¥¸¥§¥¯¥È̾¡×¤Ç¥×¥í¥¸¥§¥¯¥È¤ò¿·µ¬ºîÀ®
  3. ¡Öcd ¥×¥í¥¸¥§¥¯¥È̾¡×¤Ç¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Ë°ÜÆ°
    °Ê¹ß¤ÎÁàºî¤Ï¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Ç¹Ô¤¤¤Þ¤¹¡£

step03 Mongrel web¥µ¡¼¥Ð¤Îµ¯Æ°

  1. ¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Çruby script\server¤ò¼Â¹Ô¤·¤ÆMongrel web¥µ¡¼¥Ð¤òµ¯Æ°
    ctrl+c¥­¡¼¤ÇÃæÃǤǤ­¤Þ¤¹¡£
  2. http://127.0.0.1:3000/¤ò¥Ö¥é¥¦¥¶¤Ç³«¤¯¤È¡¢¥×¥í¥¸¥§¥¯¥È¤ÎWeb¥Ú¡¼¥¸¤¬¸«¤é¤ì¤Þ¤¹¡£

step04 Aptana Studio RadRails

UTF-8¤Ë½é´üÀßÄê

  1. Aptana Studio¤òµ¯Æ°
  2. ʸ»ú¥³¡¼¥É¤òUTF-8¤Ë½é´üÀßÄê
    [Window] - [Preferences] - [General] - [Workspace] - [Text file encoding]¤òUTF-8¤ÇÀßÄê¤ËÀßÄꤷ¤Þ¤¹¡£
    step04_RadRailsUTF8.gif

Rails¥×¥í¥¸¥§¥¯¥È¤Î¥¤¥ó¥Ý¡¼¥È

  1. [File] - [Import] - [Other] - [Import As New Project] - [Next]
    step04_RadRailsImport01.gif
  2. ¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤òÀßÄê
    step04_RadRailsImport02.gif
  3. ¤½¤Î¤Þ¤ÞFinish
    step04_RadRailsImport03.gif

step05 ¥Ç¡¼¥¿¥Ù¡¼¥¹ÀßÄê

config\database.yml¤ò³«¤¤¤Æ¡¢É¬ÍפʾðÊó¤òÆþÎÏ

development:
  adapter: mysql
  database: ¥×¥í¥¸¥§¥¯¥È̾_development
  username: root
  password:
  host: localhost

test:
  adapter: mysql
  database: ¥×¥í¥¸¥§¥¯¥È̾_test
  username: root
  password:
  host: localhost

production:
  adapter: mysql
  database: ¥×¥í¥¸¥§¥¯¥È̾_production
  username: root
  password: 
  host: localhost

MySQL¤Î´ûÄê¤Îʸ»ú¥³¡¼¥É¤òUTF-8¤ËÀßÄê

  1. [Instant Rails] - [I¥¢¥¤¥³¥ó] - [Configure] - [MySql]¤Çmy.ini¤ò³«¤¯
  2. ËöÈø¤Ë¼¡¤Î£²¹Ô¤òÄɲÃ
    default-character-set=utf8
    skip-character-set-client-handshake

step06 ¥Ç¡¼¥¿¥Ù¡¼¥¹ºîÀ®

sql¥³¥Þ¥ó¥É¤ÇºîÀ®¤¹¤ëÊýË¡

  1. phpMyAdminµ¯Æ° [Instant Rails] - [I¥¢¥¤¥³¥ó] - [Configure] - [Database(via PhpMyAdmin)]
    ¼¡¤Îurl¤ò¥Ö¥é¥¦¥¶¤Ç³«¤¤¤Æ¤âOK¤Ç¤¹¡£
    http://127.0.0.1/mysql/
  2. SQL¥¢¥¤¥³¥ó¤ò¥¯¥ê¥Ã¥¯
  3. create database ¥×¥í¥¸¥§¥¯¥È̾_development default charset utf8;
  4. ºï½ü¤¹¤ë¤È¤­¤ÏphpMyAdmin¤ò»ÈÍÑ
  5. ´Ä¶­¤òÊѤ¨¤¿DB¤òºîÀ®¤¹¤ë¤È¤­¤Ï¡¢¥³¥Þ¥ó¥É¤Ç¼¡¤Î???Éôʬ¤òtest¤äproduction¤ËÊѹ¹
    create database ¥×¥í¥¸¥§¥¯¥È̾_??? default charset utf8;

rake¥¿¥¹¥¯¤ÇºîÀ®¤¹¤ëÊýË¡

  1. lib\tasks\setup_db.rake¥Õ¥¡¥¤¥ë¤òºîÀ®
    env = ENV['RAILS_ENV'] || 'development'
    database = "¥×¥í¥¸¥§¥¯¥È̾_#{env}"
    
    task :connect_db => [:environment] do
      ActiveRecord::Base.establish_connection(
        :adapter => 'mysql',
        :host => 'localhost',
        :username => 'root',
        :database => 'mysql'
       )
    end
    
    task :setup_db => [:connect_db] do
      ActiveRecord::Schema.define do
        create_database database
      end
    end
    
    task :destroy_db => [:connect_db] do
      ActiveRecord::Schema.define do
        drop_database database
      end
    end
  2. ¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Ç¡Örake setup_db¡×
  3. ºï½ü¤¹¤ë¤È¤­¤Ï¡Örake destroy_db¡×
  4. ´Ä¶­¤òÊѤ¨¤¿DB¤òºîÀ®¤¹¤ë¤È¤­¤Ï¡Örake RAILS_ENV=test setup_db¡×¤Þ¤¿¤Ï¡Örake RAILS_ENV=production setup_db¡×

step07 migration¤Ë¤è¤ë¥Æ¡¼¥Ö¥ëºîÀ®

  1. ruby script\generate migration create_memos_table
  2. db\migrate\001_create_memos_table.rb¤òÊÔ½¸¡Ê¿ô»ú¤ÏÊѤï¤ë¾ì¹ç¤¬¤¢¤ê¤Þ¤¹¡Ë
    class CreateTable < ActiveRecord::Migration
      def self.up
        create_table :memos do |t|
          t.column :created_at, :datetime
          t.column :text, :string, :limit => 256
          t.column :location, :string, :limit => 64
        end
      end
    
      def self.down
        drop_table :memos
      end
    end
  3. ¡Örake db:migrate¡×¤Ç¥Þ¥¤¥°¥ì¡¼¥È¤ò¼Â¹Ô
  4. migration¤Ë¤è¤ê¡¢¸åÆüÎ㤨¤Ðʸ»ú¿ô¤òÊѹ¹
    ruby script\generate migration expand_location
  5. db\migrate\002_expand_location.rb¤òÊÔ½¸¡Ê¿ô»ú¤ÏÊѤï¤ë¾ì¹ç¤¬¤¢¤ê¤Þ¤¹¡Ë
    class ExpandLocation < ActiveRecord::Migration
      def self.up
      change_column :memos, :location, :string, :limit => 256
      end
    
      def self.down
      change_column :memos, :location, :string, :limit => 64
      end
    end
  6. ¡Örake db:migrate¡×¤Ç¥Þ¥¤¥°¥ì¡¼¥È¤ò¼Â¹Ô
  7. ¥Ð¡¼¥¸¥ç¥ó£±¤ËÌ᤹¤È¤­¤Ï¡¢
    rake db:migrate VERSION=1

step08 scaffold¤Ë¤è¤ê¥â¥Ç¥ë¤ª¤è¤Ó¥â¥Ç¥ë¤Ë¥¢¥¯¥»¥¹¤¹¤ë­¾ì¤Î¥Ú¡¼¥¸¤òºîÀ®

¥×¥í¥¸¥§¥¯¥È¥ë¡¼¥È¤Ç¼¡¤Î¥³¥Þ¥ó¥É¤òÆþÎÏ

ruby script\generate scaffold ¥â¥Ç¥ë̾
¢¨¥â¥Ç¥ë̾¤Ï¥¢¥Ã¥Ñ¡¼¥­¥ã¥á¥ë¤Îñ¿ô·Á¤Ç¤¹

Î㤨¤Ð¡Öruby script\generate scaffold Memo¡×¤È¤·¤¿¾ì¹ç¡¢¼¡¤Î¥Ú¡¼¥¸¤¬ºîÀ®¤µ¤ì¤Þ¤¹

¢¨¥³¥ó¥È¥í¡¼¥é¡¼¤¬memos¡¢¥¢¥¯¥·¥ç¥ó¤¬list, new, show, edit, destroy¤È¤Ê¤ê¤Þ¤¹

¼¡¤Î¥Õ¥¡¥¤¥ë¤¬ºî¤é¤ì¤Þ¤¹¡£

  • app\models\memo.rb ... ¥â¥Ç¥ë
  • app\controllers\memos_controller.rb ... ¥³¥ó¥È¥í¡¼¥é¡¼
  • app\views\layouts\memos.rhtml ... Web¥¢¥×¥êÁ´ÂΤΥӥ塼
  • app\views\memos\edit.rhtml ... edit¥¢¥¯¥·¥ç¥ó¤Î¥Ó¥å¡¼¡ÊÊÔ½¸¤Î¥Ó¥å¡¼¡Ë
  • ...

step09 ¥â¥Ç¥ë¤Ø¤Î¸¡¾Ú½èÍýÄɲÃ

¥â¥Ç¥ë¤Ë¸¡¾Ú¥á¥½¥Ã¥É¤òÄɲä·¤Þ¤¹¡£
app\models\memo.rb

class Memo < ActiveRecord::Base
  validates_presence_of :text, :location
  validates_length_of :location, :maximum => 256
  validates_length_of :¥Õ¥£¡¼¥ë¥É̾, :within 6..12
end

ºÇ¸å¤ÎÎã¤Ï6ʸ»ú°Ê¾å12ʸ»ú°Ê²¼¤Ç¤¹¡£
¥É¥Ã¥È¤ò£³¤Ã¤Ä¤È¤¹¤ë¤È¡Ö̤Ëþ¡×¤È¤Ê¤ê¤Þ¤¹¡£

step10 ·îÁªÂò¤Î¥Õ¥©¡¼¥à¤ò±Ñ¸ì¤«¤é¿ô»ú¤ËÊѹ¹

app\views\memos\_form.rhtml

...
<%= datetime_select 'memo', 'created_at', :use_month_numbers => true %></p>
...

_form.rhtml¤«¤é¡Öcreated_at¡×¤Îɽ¼¨¤ò´Ý¤´¤Èºï½ü¤¹¤ë¤È¡¢¼«Æ°Åª¤Ë¥Ç¡¼¥¿ºîÀ®Æü»þ¤¬ÀßÄꤵ¤ì¤Þ¤¹¡£

step11 ¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤¿gem¤ÎAPI¥É¥­¥å¥á¥ó¥È»²¾È

  1. DOS¥×¥í¥ó¥×¥È¤«¤égem_server¤ÈÆþÎÏ
  2. ¥Ö¥é¥¦¥¶¤Ç¼¡¤Îurl¤ò³«¤¯
    http://127.0.0.1:8808/
    Î㤨¤Ð¸¡¾Ú¤ÇÍѤ¤¤ë¥á¥½¥Ã¥É¤Ï¼¡¤ÎAPI¤ò»²¾È¤·¤Þ¤¹¡£
    [activerecord rdoc] - [ActiveRecord::Validations::ClassMethods]

step12 Ruby-GetText¤Î¥¤¥ó¥¹¥È¡¼¥ë

  1. ¡Øgettext-1.10.0-mswin32.gem¡Ù¤òÆþ¼ê
  2. Instant Rails¤òµ¯Æ°¤·¡¢¤½¤³¤«¤éDOS¥×¥í¥ó¥×¥È¤ò³«¤¯
  3. ¥³¥Þ¥ó¥É¤òÆþÎϤ·¤Æ¥¤¥ó¥¹¥È¡¼¥ë
    gem install C:\tmp\gettext-1.10.0-mswin32.gem
    È÷¹Í¡§
    • ¡Ögem install gettext¡×¤ÈÆþÎϤ·¤Æ¡¢¥ª¥ó¥é¥¤¥ó¼èÆÀ¤·¤¿¥á¥Ë¥å¡¼¤«¤éÁªÂò¤¹¤ë¥¤¥ó¥¹¥È¡¼¥ëÊýË¡¤â¤¢¤ë¤½¤¦¤Ç¤¹¤¬¡¢¤Ê¤¼¤«¡ÖGemNotFoundException¡×¤È¤Ê¤ê¥¤¥ó¥¹¥È¡¼¥ë¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£
    • ¤³¤ì¤è¤ê¿·¤·¤¤¥Ð¡¼¥¸¥ç¥ó¤ÎRuby-GetText¤ÏRails 2.0¤Î¤ßÂбþ¤Ç¤¹
    • ¥¤¥ó¥¹¥È¡¼¥ëÀ衧C:\InstantRails\ruby\lib\ruby\gems\1.8\gems\gettext-1.10.0-mswin32
  4. GNU GetText¤ò¥¤¥ó¥¹¥È¡¼¥ë
    ¡Øruby-gnome2-0.16.0-1-i386-mswin32.exe¡Ù¤òÆþ¼ê¤·¤Æ¥¤¥ó¥¹¥È¡¼¥ë¤·¤Þ¤¹¡£
    ¥¤¥ó¥¹¥È¡¼¥ëÀ衧C:\InstantRails\ruby\Ruby-GNOME2
  5. Instant Rails¤òºÆµ¯Æ°

step13 Ruby-GetText¤òÍøÍѤ¹¤ë¤è¤¦¥×¥í¥¸¥§¥¯¥È¤ËÀßÄê

  1. ¡Øgettext.rake¡Ù¤òlib\tasks¤Ë¥³¥Ô¡¼
  2. script\server¥³¥Þ¥ó¥É¤ò¼Â¹ÔÃæ¤Î¤È¤­¤Ïctrl+c¥­¡¼¤ÇÄä»ß
    config¥Õ¥©¥ë¥ÀÆâ¤Î¥Õ¥¡¥¤¥ë¤òÊѹ¹¤¹¤ë¤È¤­¤ÏºÆµ¯Æ°¤¬É¬Íפʤ¿¤á¤Ç¤¹¡£
  3. ¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Ç¡Örake init_gettext¡×¤ò¼Â¹Ô
    ¸µ¤ËÌ᤹¤È¤­¤Ï¡Örake void_gettext¡×¤ò¼Â¹Ô
  4. ruby script\server¤ÇWeb¥µ¡¼¥ÐºÆµ¯Æ°

step14 Ruby-GetText¤òÍøÍѤ·¤ÆÆüËܸ첽

  1. ¥Ó¥å¡¼¤Î½ñ¤­´¹¤¨Îã
    app\views\memos\_form.rhtml¤ò¼¡¤Î¤è¤¦¤Ë½ñ¤­´¹¤¨
    <label for="memo_created_at">Create at</label>
            ¢­
    <label for="memo_created_at"><%=h _('Create at')%></label>
    app\controllers\memos_controller.rb¤ò¼¡¤Î¤è¤¦¤Ë½ñ¤­´¹¤¨
          flash[:notice] = 'Memo was successfully created.'
            ¢­
          flash[:notice] = _('Memo was successfully created.')
  2. pot/po¥Õ¥¡¥¤¥ëºîÀ®
    rake update_po
  3. po¥Õ¥¡¥¤¥ë¤Ëɽ¼¨Ê¸¸À¤òÄêµÁ
    po\ja\memopad.po½ñ¤­´¹¤¨Îã
    #: app/views/memos/_form.rhtml:4
    msgid "Create at"
    msgstr "ºîÀ®ÆüÉÕ"
    ...
    #: app/controllers/memos_controller.rb:26
    msgid "Memo was successfully created."
    msgstr "¥á¥â¤ÎºîÀ®¤ËÀ®¸ù¤·¤Þ¤·¤¿"
  4. mo¥Õ¥¡¥¤¥ëºîÀ®
    rake make_mo

step15 ¥Æ¥¹¥È¶îÆ°³«È¯(TDD, Test Driven Development)¤Î½àÈ÷

  1. ¥Æ¥¹¥ÈÍÑDBºîÀ®
    ¼¡¤Î¤É¤Á¤é¤«¤ÇºîÀ®¤·¤Þ¤¹¡£
    • phpMyAdmin SQL¥³¥Þ¥ó¥ÉÆþÎϤÇ
      create database ¥×¥í¥¸¥§¥¯¥È̾_test default charset utf8;
    • DOS¥×¥í¥ó¥×¥È ¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Ç¤Î¥³¥Þ¥ó¥ÉÆþÎϤÇ
      rake RAILS_ENV=test setup_db
  2. ¥Æ¥¹¥È¤ò¼Â¹Ô
    ¥×¥í¥¸¥§¥¯¥È¥Õ¥©¥ë¥À¤Ç¡Örake¡×
  3. ÀÅŪ¥Æ¥¹¥È¥Ç¡¼¥¿ÍÑ°Õ
    test\fixtures\memos.yml
    one:
      id: 1
      text: ¥Æ¥¹¥È¥Ç¡¼¥¿#1
      location: ¼«Âð
    two:
      id: 2
      text: ¥Æ¥¹¥È¥Ç¡¼¥¿#2
      location: Ï©¾å
    ¥Æ¥¹¥È¥Ç¡¼¥¿ ¥×¥í¥°¥é¥àÀ¸À®Îã
    today: <%= Date.today.to_s %>
    created_at: <%= Time.now.to_formatted_s :db %>
  4. ưŪ¥Æ¥¹¥È¥Ç¡¼¥¿ÍÑ°Õ
    test\functional\memos_controller_test.rb
      ...
      def setup
        @controller = MemosController.new
        @request    = ActionController::TestRequest.new
        @response   = ActionController::TestResponse.new
    
        @first_id = memos(:one).id ¢«¤³¤Î¤è¤¦¤Ëfirst¤«¤éone¤Ë½ñ¤­´¹¤¨
      end
      ...
      def test_create
        num_memos = Memo.count
    
    ### post :create, :memo => {}
    ###     ¢­¤³¤Î¤è¤¦¤Ë¥Æ¥¹¥È¥Ç¡¼¥¿¤òÄêµÁ
        post :create, :memo => {:text => '¥Æ¥¹¥È¥Ç¡¼¥¿#3', :location => '¥Æ¥¹¥È'}
    
        assert_response :redirect
        assert_redirected_to :action => 'list'
    
        assert_equal num_memos + 1, Memo.count
      end
      ...
  5. ºÆ¤Ó¡Örake¡×¤Ç¥Æ¥¹¥È¤ò¼Â¹Ô¤·¡¢failures, errors¤¬£°¤È¤Ê¤ë¤³¤È¤ò³Îǧ

step16 ¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¡Ê¾Êά¡Ë

¡Ø10Æü¤Ç¤ª¤Ü¤¨¤ëRuby on Rails¡Ù148¥Ú¡¼¥¸¤ò¤´»²¾È¤¯¤À¤µ¤¤¡£

step17 ¥Õ¥¡¥ó¥¯¥·¥ç¥Ê¥ë¥Æ¥¹¥È¡Ê¾Êά¡Ë

¡Ø10Æü¤Ç¤ª¤Ü¤¨¤ëRuby on Rails¡Ù164¥Ú¡¼¥¸¤ò¤´»²¾È¤¯¤À¤µ¤¤¡£

step18 ¥Ó¥å¡¼½ñ¤­´¹¤¨Î㡧ÆþÎϤ·¤¿ÆâÍƤ¬'http://'¤Ç»Ï¤Þ¤ë¤È¤­¤Ë¥ê¥ó¥¯¤È¤·¡¢ÆüÉÕyyyy-mm-dd hh:mm¤ËÊѹ¹

app\views\memos\list.rhtml

<h1><%=h _('Listing memos') %></h1>

<table border="1">
  <tr>
  <% for column in Memo.content_columns %>
    <th><%= column.human_name %></th>
  <% end %>
  </tr>
  
<% for memo in @memos %>
  <tr>
  <% for column in Memo.content_columns %>
    <% if column.name == 'location' && memo.location.index('http://') == 0 %> ... ¤³¤Î¤è¤¦¤Ë½ñ¤­´¹¤¨
      <td><%= link_to h(memo.location), memo.location %></td> ... ¥ê¥ó¥¯¤Î½ñ¤­Êý¤Ç¤¹¡£
                                                                  ɽ¼¨ÆâÍƤÏh()¤Ç¥¨¥¹¥±¡¼¥×¤·¤Þ¤¹¡£
    <% else %>
      <td><%=h memo.send(column.name) %></td> ... send()¤òÍѤ¤¤ë¤È¥Õ¥£¡¼¥ë¥É̾¤ò·è¤á¤º¤Ë¥ë¡¼¥×½èÍý¤Ç¤­¤Þ¤¹¡£
                                                  Î㤨¤Ðcolumn.name¤¬'text'¤Î¤È¤­¡¢memo.send(column.name)¤Ï
                                                  memo.text¤È¤Ê¤ê¤Þ¤¹¡£
    <% end %>
  <% end %>
    <td><%= link_to _('Show'), :action => 'show', :id => memo %></td>
    <td><%= link_to _('Edit'), :action => 'edit', :id => memo %></td>
    <td><%= link_to _('Destroy'), { :action => 'destroy', :id => memo }, :confirm => _('Are you sure?'),  :method => :post %></td>
  </tr>
 <% end  %>
</table >

<%= link_to _('Previous page'), { :page => @memo_pages.current.previous } if @memo_pages.current.previous  %>
<%= link_to _('Next page'), { :page => @memo_pages.current.next } if @memo_pages.current.next %>

<br />

<%= link_to _('New memo'), :action => 'new' %>


¡¡¡¡¡¡¡¡¢­
¤µ¤é¤Ë¥í¥¸¥Ã¥¯Éôʬ¤ò¥Ø¥ë¥Ñ¡¼¥á¥½¥Ã¥É¤Ë³°½Ð¤·¤·¡¢ÆüÉÕ¤â½ñ¤­´¹¤¨ app\views\memos\list.rhtml

#pre{{
...
<% for column in Memo.content_columns %>
<td><%= format_column_value memo, column.name %></td>
<% end %>
...
}}

app\helpers\memos_helper.rb

#pre{{
module MemosHelper
# ¥á¥â¥ª¥Ö¥¸¥§¥¯¥È¤Î»ØÄꥫ¥é¥à¤òɽ¼¨ÍѤËÀ°·Á
def format_column_value(memo, colname)
if colname == 'location' && memo.location.index('http://') == 0
link_to h(memo.location), memo.location
elsif colname == 'created_at'
memo.created_at.strftime '%Y-%m-%d %H:%M' if memo.created_at # ÆüÉդ⤳¤Î¤è¤¦¤ËÀ°·Á
else
h memo.send(colname)
end
end
end
}}

step19 ¥â¥Ç¥ëÄɲÃÎã: źÉÕ¥Õ¥¡¥¤¥ë¤òɽ¤¹Attachment¥â¥Ç¥ë¤òÄɲä·¤Þ¤¹¡£

  1. ¥â¥Ç¥ë¤òÀ¸À®
    C:\InstantRails\rails_apps\memopad>ruby script\generate model Attachment
          exists  app/models/
          exists  test/unit/
          exists  test/fixtures/
          create  app/models/attachment.rb
          create  test/unit/attachment_test.rb
          create  test/fixtures/attachments.yml
          exists  db/migrate
          create  db/migrate/003_create_attachments.rb ... ¥â¥Ç¥ë¤òÀè¤ËºîÀ®¤¹¤ë¤È¡¢¤³¤Î¤è¤¦¤Ë¥Æ¡¼¥Ö¥ë
                                                           ºîÀ®¡¦ºï½ü¤Îmigration¥Õ¥¡¥¤¥ë¤Ò¤Ê·Á¤¬ºî¤é¤ì¤Þ¤¹
  2. ¥Õ¥£¡¼¥ë¥É¤òÄêµÁ
    db\migrate\003_create_attachments.rb
    class CreateAttachments < ActiveRecord::Migration
      def self.up
        create_table :attachments do |t|
          t.column :memo_id, :integer                   # ¤³¤Î¤è¤¦¤ËÄɲä·¤Þ¤¹
          t.column :name, :string, :limit => 64
          t.column :size, :integer
          t.column :content_type, :string, :limit => 64
          t.column :content, :mediumblob
        end
      end
    
      def self.down
        drop_table :attachments                         # Àè¤Ë¥â¥Ç¥ë¤òÀ¸À®¤¹¤ë¤È¡¢¤³¤Î¤è¤¦¤Ë¥Æ¡¼¥Ö¥ë
                                                        # ºï½ü¤â¼«Æ°¤ÇÆþ¤Ã¤ÆÊØÍø¤Ç¤¹
      end
    end
  3. migration¼Â¹Ô
    rake db:migrate
  4. »Ò¦¡§Attachment¥â¥Ç¥ë¤Ë¸¡¾Ú¡¢belongs_to¥á¥½¥Ã¥É¡¢¥á¥¿¥Ç¡¼¥¿¼èÆÀ¥á¥½¥Ã¥É¤òÄɲÃ
    app\models\attachment.rb

    #pre{{
    class Attachment < ActiveRecord::Base
    # ¸¡¾Ú½èÍý
    validates_presence_of :name, :size, :content_type, :content

    # memo¥ª¥Ö¥¸¥§¥¯¥È¤Ë½ê°¤¹¤ë¤³¤È¤òÀë¸À
    # ¤³¤ì¤Ë¤è¤Ã¤Æ¤³¤Î¥¯¥é¥¹¤«¤é¿ÆMemo¤Î°À­¤¬»ÈÍѤǤ­¤Þ¤¹
    # memo¤Ïñ¿ô¤Ê¤Î¤Çñ¿ô·Á¤Ç¤¹¡£
    belongs_to :memo

    # ¥á¥¿¥Ç¡¼¥¿¥«¥é¥à̾
    # źÉÕ¥Õ¥¡¥¤¥ë¼«¿È°Ê³°¡¢¤¹¤Ê¤ï¤ÁÍÆÎ̤¬¾®¤µ¤¤¥«¥é¥à¤Î¥«¥é¥à̾¤Ç¤¹¡£
    METADATA_COLUMNS = 'id, memo_id, name, size, content_type'

    # Memo¥ª¥Ö¥¸¥§¥¯¥È¤ò»ØÄꤷ¤Æ¡¢ÅºÉÕʪ¥á¥¿¥Ç¡¼¥¿ÇÛÎó¤Î¼èÆÀ¤Î¥á¥½¥Ã¥É
    # @param ¿Æ¤ÎMemo¥ª¥Ö¥¸¥§¥¯¥È
    # @return źÉÕʪ¥á¥¿¥Ç¡¼¥¿ÇÛÎó
    # Î㡧arMeta = Attachment.metadatas memos(objMemo)
    # puts arMeta.size
    # if (arMeta.size > 0)
    # puts arMeta[0].memo_id
    # end
    def self.metadatas(memo)
    find :all, :select => METADATA_COLUMNS,
    :conditions => ['memo_id = ?', memo.id]
    end

    # źÉÕʪid¤«¤é¥á¥¿¥Ç¡¼¥¿¼èÆÀ¤Î¥á¥½¥Ã¥É
    # @param źÉÕʪid
    # @return ¤½¤Îid¤ò»ý¤ÄźÉÕʪ¥á¥¿¥Ç¡¼¥¿
    def self.metadata(id)
    find id, :select => METADATA_COLUMNS
    end
    end
    }}

  5. ¿Æ¦¡§Memo¥â¥Ç¥ë¤Ëhas_many¥á¥½¥Ã¥É¤òÄɲÃ

    #pre{{
    class Memo < ActiveRecord::Base
    validates_presence_of :text, :location
    validates_length_of :location, :maximum => 256

    # attachments¥ª¥Ö¥¸¥§¥¯¥È¤òÊ£¿ô»ý¤Á¡¢:select¤Ç»ØÄꤷ¤¿¥«¥é¥à¤Ë¥¢¥¯¥»¥¹¤Ç¤­¤ë¤³¤È¤òÀë¸À
    # attachments¤ÏÊ£¿ô¤Ê¤Î¤ÇÊ£¿ô·Á¤Ç¤¹¡£
    # Â裲°ú¿ô¤Ë¤Ï:order¤Ê¤Éfind¥á¥½¥Ã¥É¤ÈƱ¤¸°ú¿ô¤ò»ØÄê¤Ç¤­¤Þ¤¹¡£
    has_many :attachments, :select => Attachment::METADATA_COLUMNS

    def self.bookmarks
    find :all, :conditions => "location like 'http://%'", :order => "created_at DESC"
    end
    end
    }}

step20 źÉÕ¥Õ¥¡¥¤¥ë¤Î¥¢¥Ã¥×¥í¡¼¥É

¥Ó¥å¡¼½ñ¤­´¹¤¨

app\views\memos\new.rhtml

#pre{{
<h1><%=h _('New memo')%></h1>

<% form_tag ({:action => 'create'}, :multipart => true) do %> ... multipart¤òÀßÄê
<%= render :partial => 'form' %> ... ¤³¤ì¤Ï¸µ¤«¤é¤¢¤ë¥½¡¼¥¹¤Ç¡¢_form.rhtml¤òÆɤ߹þ¤ß¤Þ¤¹
<p>
<label for="file"><%=h _('Attachment') %><label><br /> ... ¥é¥Ù¥ë
<%= file_field_tag 'file' %> ... ¥Õ¥¡¥¤¥ë̾ÆþÎϤȻ²¾È¥Ü¥¿¥ó
</p>
<%= submit_tag _("Create") %>
<% end %>

<%= link_to _('Back'), :action => 'list' %>
}}

źÉÕ¥Õ¥¡¥¤¥ëÆþ¤ê¤Ç¥â¥Ç¥ëÀ¸À®

app\controllers\memos_controller.rb

#pre{{
...
def create
@memo = Memo.new(params[:memo])
if params[:file] && params[:file].respond_to?(:original_filename)
# create¥¢¥¯¥·¥ç¥ó¤Ëfile¥Ñ¥é¥á¡¼¥¿¤¬Æþ¤Ã¤Æ¤¤¤¿¤È¤­¤Î½èÍý
@memo.attachments.create :name => params[:file].original_filename,
:size => params[:file].length,
:content_type => params[:file].content_type,
:content => params[:file].read
end

if @memo.save
flash[:notice] = _('Memo was successfully created.')
redirect_to :action => 'list'
else
render :action => 'new'
end
end
...
}}

step21 źÉÕ¥Õ¥¡¥¤¥ëɽ¼¨Éôʬ¤òÄɲÃ

¥ë¡¼¥Æ¥£¥ó¥°¤ÎÀßÄê

config\routes.rb
¼¡¤ÎÀèƬ£²¹Ô¤òÄɲÃ

 map.connect ':controller/:action/:id/:filename.:fileext'
 map.connect ':controller/:action/:id/:filename'
 map.connect ':controller/:action/:id.:format'
 map.connect ':controller/:action/:id'

¥á¥â¤Îɽ¼¨¤ËźÉÕ¥Õ¥¡¥¤¥ë¥ê¥¹¥Èɽ¼¨

app\views\memos\show.rhtml

#pre{{
...
<% if !@memo.attachments.empty? %>
<b><%=h _('Attachments') %>:</b>
<table border="1">
<tr>
<% for column in @memo.attachments.content_columns %>
<th><%= column.human_name %></th>
<% end %>
</tr>
<% for attachment in @memo.attachments %>
<tr>
<% for column in @memo.attachments.content_columns %>
<td><%= format_column_value attachment, column.name %></td>
<% end %>
</tr>
<% end %>
</table>
<% end %>
...
}}

¥í¥¸¥Ã¥¯Éôʬ¤Ï¥Ø¥ë¥Ñ¡¼¤Ëµ­½Ò

app\helpers\memos_helper.rb

#pre{{
module MemosHelper
# ¥ª¥Ö¥¸¥§¥¯¥È¤Î»ØÄꥫ¥é¥à¤òɽ¼¨ÍѤËÀ°·Á
def format_column_value(ar, colname)
if Memo === ar
format_memo_column_value ar, colname
elsif Attachment === ar
format_attachment_column_value ar, colname
end
end

# ¥á¥â¥ª¥Ö¥¸¥§¥¯¥È¤Î»ØÄꥫ¥é¥à¤òɽ¼¨ÍѤËÀ°·Á
def format_memo_column_value(memo, colname)
...
end

# źÉÕ¥ª¥Ö¥¸¥§¥¯¥È¤Î»ØÄꥫ¥é¥à¤òɽ¼¨ÍѤËÀ°·Á
def format_attachment_column_value(atch, colname)
if colname == 'content'
if atch.content_type =~ /image\/(x-|p)?(jpe?g|png|gif)/
image_tag url_for(:action => 'file', :id => atch.id, :filename => atch.name), :alt => h(atch.name)
else
link_to h(atch.name), {:action => 'file', :id => atch.id, :filename => atch.name}
end
else
h atch.send(colname)
end
end
...
end
}}

file¥¢¥¯¥·¥ç¥ó¤ÎÄɲÃ

app\controllers\memos_controller.rb

#pre{{
...
def file
attachment = Attachment.find params[:id]
# return if forbidden?(attachment.memo)
filename = (params[:fileext]) ? "#{params[:filename]}.#{params[:fileext]}" : params[:filename]
if filename != attachment.name
render :file => File.join(RAILS_ROOT, 'public', '404.html'), :status => 404, :layout => true
else
disposition = 'inline'
send_data attachment.content, :filename => attachment.name, :type => attachment.content_type, :disposition => disposition
# cache_page if attachment.memo.public_memo
end
end
...
}}