2012-12-14

Ramaze Simple Tutorial 2 (with Sequel)



이 튜토리얼에서는 migration 을 사용하지 않고, Model 에서 직접 스키마를 작성하고 Table 을 생성하도록 하였다. 그리고, Ramaze (2012.12.08) 에서 테스트를 진행하였다.

  • 관련 모듈 설치
    $ apt-get install libsqlite3-dev sqlite3
    $ gem install ramaze sequel thin sqlite3
    
  • 프로젝트 생성
    $ mkdir -p /opt/project/ramaze
    $ cd /opt/project/ramaze
    $ ramaze create web
    
  • 기본 웹서버 변경 -> task/ramaze.rake 에서 :webrick 을 :thin 으로 수정
  • 웹서버 시작 테스트
    $ cd /opt/project/ramaze/web
    $ thin start
    
  • db / model 폴더 생성
    $ cd /opt/project/ramaze/web
    $ mkdir -p db
    $ mkdir -p model
    
  • app.rb 에 다음 추가
    require 'sequel'
    
    # Open the accounts database
    DB = Sequel.connect('sqlite://db/web.db')
    
    # Initialize controllers and models
    require __DIR__('model/init')
    require __DIR__('controller/init')
    
  • model/init.rb 에 다음 추가
    require __DIR__('admin')
    
  • model/admin.rb 생성
    # coding : utf-8
    require 'sequel'
    
    class Admin < Sequel::Model(:admins)
      plugin :schema
      
      set_schema do
        primary_key :id
    
        String :login, :unique => true, :empty => false
        String :password, :empty => false
        String :name
        String :email
        String :mobile_phone
        String :level, :default => '00'
      end
    
      # Create the database table if it doesn't exists
      # Also add a admin
      if ! table_exists?
        create_table
        create :login => 'hong', :password => 'hong1234', :name => '홍길동', :email => 'hong@test.com', :mobile_phone => '010-5555-5555', :level => '00'
        create :login => 'jang', :password => 'jang1234', :name => '장영실', :email => 'jang@test.com', :mobile_phone => '010-6666-6666', :level => '01'
      end
    end
    
  • controller/init.rb 에 다음을 추가
    require __DIR__('admin')
    
  • controller/admin.rb 생성
    class AdminController < Controller
      map '/admin'
      def index
        'Admin Test'
      end
    end
    
  • 프로젝트를 실행하고 브라우저에서 http://localhost:3000/admin 을 접근해본다.
  • 이제, controller/admin.rb 의 index 를 다음과 같이 변경한다.
      def index
        @admins = Admin.all
      end
    
  • view 디렉토리 생성
    $ cd /opt/project/itsm_ramaze/web/view
    $ mkdir admin
    
  • view/admin/index.xhtml 생성
    <h1> Admin Test Page </h1>
    <?r @admins.each do |admin| ?>
      #{admin.login}
      #{admin.name}
      #{admin.email}
      #{admin.mobile_phone}
      #{admin.level}
      <br/>
    <?r end ?>
    
  • 프로젝트를 실행하고 브라우저에서 http://localhost:3000/admin 을 접근해서 데이터 목록이 뜨면 정상이다.

댓글 없음:

댓글 쓰기