지난시간에 맛만 보여드린 Ruby on rails에 대해서 조금 더 설명드리겠습니다.

어디서부터 어디까지 설명해야 할지 잘 모르겠네요.

 

1. Ruby on rails와 Ruby는 다른것인가?

  Ruby는 1990년대 중반에 어떤 일본 사람이 만든 Object-oriented 형식의 프로그래밍 언어입니다. 진정한 object-oriented 컨셉을 구현한, 사용하기 쉬운 script 방식의 언어를 만들고 싶어서 만들었다네요. 만들어진 직후에는 그다지 반응이 없었다고 알고 있습니다.

일본 내에서 자국용으로 주로 사용되던 Ruby를 해외에서도 유명하게 해 준 것은 Ruby on rails라는 ruby 언어로 만들어진 웹 어플리케이션 프레임워크입니다.

2. Ruby on rails란?

 위에서 말씀드린 것처럼 웹 어플리케이션 프레임워크입니다.

 MVC(Model-View-Controller) 구조를 명확하게 구현하고 있으며, 여타 프레임워크에 비해 사람이 손 댈 부분이 굉장히 적습니다. Ruby on rails의 인기도 거기에 기반한 부분이 크구요. 예를 들어 'blog in less than 10 minutes using Ruby on rails(http://www.youtube.com/watch?v=JaL9ul17kx0)'와 같은 단시간 내 ruby on rails를 사용하여 구현하는 작업들이 유명합니다.

 

3. 예제

앞에 올린 게시물에서 ruby on rails 설치까지 진행하였는데요.

mysql을 사용하는 sarc 이라는 이름의 프로젝트를 만들어 보겠습니다.

[root@localhost]# rails sarc --database mysql
      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  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_controller.rb
      create  app/helpers/application_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/locales/en.yml
      create  db/seeds.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/initializers/session_store.rb
      create  config/environment.rb
      create  config/boot.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/runner
      create  script/server
      create  script/plugin
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      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

위에서 보는 것처럼 'rails sarc'만 입력하면 웹어플리케이션 용 기본적인 뼈대를 자동으로 생성해 줍니다.

앞서 rails는 MVC 구조라고 말씀드렸습니다. 간단한 model과 viewer, controller를 설정해 주고 해당 프로젝트를 실행하면 웹 페이지를 바로 만들 수 있습니다.

먼저 사용할 데이터베이스 정보를 입력합니다.

[root@localhost]# cd sarc/config
[root@localhost]# vi database.yml
test:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: sarc_test
  pool: 5
  username: sarc
  password: test
  socket: /var/lib/mysql/mysql.sock
production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: sarc_production
  pool: 5
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock

그리고 해당 데이터베이스 정보를 가지고 model과 controller를 작성합니다.

[root@localhost sarc]# ruby script/generate model sarc
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/sarc.rb
      create  test/unit/sarc_test.rb
      create  test/fixtures/sarcs.yml
      create  db/migrate
      create  db/migrate/20140610151050_create_sarcs.rb
[root@localhost sarc]# ruby script/generate controller sarc
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/sarc
      exists  test/functional/
      create  test/unit/helpers/
      create  app/controllers/sarc_controller.rb
      create  test/functional/sarc_controller_test.rb
      create  app/helpers/sarc_helper.rb
      create  test/unit/helpers/sarc_helper_test.rb

rails generate scarffold 라는 명령어를 사용해서 보다 쉽게 만들 수도 있다고 합니다.

이제 껍데기는 대충 만들어 졌습니다.

껍데기라도 제대로 동작하는지 확인하고 싶으시다면 아래와 같이 sarc pjt를 기동하여 호출해 봅니다.

[root@localhost sarc]# ruby script/server
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

호출 주소는 http://localhost:3000/sarc 으로 호출하시면 정의된 action이 없다는 메시지가 뜰 것입니다.

hello, World!를 보고 싶으신 분은 아래 경로에 index.html.erb를 만들어서 아래 내용을 넣어주시면 됩니다.

[root@localhost sarc]# cd app/views/sarc/
[root@localhost sarc]# vi index.html.erb
<h1>Hello, WORLD!</h1>

다음 시간에는 실제 데이터를 사용해서 동작하는 모습을 보여드릴까 합니다. :)