dhtmlxGantt와 Ruby on Rails 연동하기
이 문서에서는 Ruby on Rails 백엔드를 사용하여 Gantt 차트를 만드는 과정을 안내합니다. 예제에서는 Ruby 2.4.1, Rails 5.1.3, 그리고 MySQL을 사용합니다. 이 사전 조건들이 이미 설치되어 있다고 가정합니다. 만약 설치되어 있지 않다면, 공식 튜토리얼을 먼저 참고하시기 바랍니다.
다른 기술 스택을 사용 중이라면, 아래의 통합 옵션을 참고할 수 있습니다:
- dhtmlxGantt와 ASP.NET Core 사용하기
- dhtmlxGantt와 ASP.NET MVC
- dhtmlxGantt와 Node.js 연동하기
- dhtmlxGantt와 Python
- dhtmlxGantt와 PHP: Laravel 연동
- dhtmlxGantt와 PHP:Slim 연동하기
- dhtmlxGantt와 Salesforce LWC 연동하기
데모는 GitHub에서도 제공됩니다: https://github.com/DHTMLX/gantt-howto-rails.
1단계. 프로젝트 생성
새 프로젝트를 생성하려면 터미널에서 다음 명령어를 실행하세요:
rails new gantt-app -d mysql
2단계. 페이지에 Gantt 추가하기
먼저 컨트롤러와 앱의 기본 페이지를 생성합니다. 애플리케이션 폴더로 이동한 뒤, index 액션이 포함된 새로운 컨트롤러를 생성하세요:
cd gantt-app
rails generate controller gantt index
새 파일이 생성되었다는 메시지가 표시됩니다.
기본 라우트 설정
라우팅을 설정하려면 config/routes.rb 파일을 열고, 기본 라우트를 새 컨트롤러의 "index" 액션을 가리키도록 변경하세요:
config/routes.rb
Rails.application.routes.draw do
root :to => "gantt#index"
end
이제 서버를 실행해보세요:
rails server
그리고 브라우저에서 http://localhost:3000/ 을 엽니다. 아래와 같이 빈 페이지가 나타납니다:

앱이 실행 중이고 기본 페이지가 준비되었으니, 다음 단계는 Gantt 차트를 추가하는 것입니다.
View에 Gantt 추가하기
이제 페이지에 Gantt 차트를 삽입할 차례입니다.
레이아웃 파일을 열고 head 태그 내부에 yield를 추가하세요. 이렇게 하면 dhtmlxGantt 파일을 포함할 수 있습니다:
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>dhtmlxGantt</title>
(= stylesheet_link_tag 'application', media:'all','data-turbolinks-track' => true )
(= javascript_include_tag 'application', 'data-turbolinks-track' => true )
(= yield(:head) ) /*!*/
(= csrf_meta_tags )
</head>
<body>
(= yield )
</body>
</html>
다음으로, gantt/index 뷰를 열고 Gantt 차트를 추가하세요:
app/views/gantt/index.html.erb
( content_for :head do )
(= stylesheet_link_tag 'https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css' )
(= javascript_include_tag 'https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js' )
( end )
<div id="gantt_here" style='width:100%; height:800px;'></div>
<script>
gantt.init("gantt_here");
</script>
여기서는 dhtmlxGantt 파일을 CDN에서 불러옵니다. 개발 시에는 다운로드 패키지에 포함된 소스 파일을 사용할 수도 있습니다.
이제 http://localhost:3000/ 을 다시 열어보세요. 다음과 같은 화면이 나타납니다:

이제 작업을 추가 및 편집할 수 있는 Gantt 차트가 나타납니다. 아직 저장 기능은 구현되지 않았으므로, 다음 단계에서 모델을 생성합니다.
3단계. 모델 생성하기
MySQL을 사용하므로, config/database.yml에서 연결 설정이 올바른지 확인하세요. 예시:
config/database.yml
development:
adapter: mysql2
encoding: utf8
host: localhost
database: gantt-app
username: root
password:
이제 tasks와 links를 위한 모델을 생성해야 합니다.
Task 모델을 생성하려면 다음 명령어를 실행하세요:
rails generate model Task
text:string
start_date:datetime
duration:integer
parent:integer
progress:decimal
마찬가지로, Link 모델을 아래 명령어로 생성하세요:
rails generate model Link
source:integer
target:integer
link_type:string:limit1
dhtmlxGantt의 link 객체는 관계 유형(예: start-to-start, finish-to-finish 등)을 지정하는 type 속성이 필요합니다.
하지만 ActiveRecord에서 "type"은 예약어이므로, 여기서는 link_type으로 이름을 지정하고 컨트롤러에서 매핑 처리를 합니다.
필수 및 선택 속성 전체 목록은 Task object 및 Link object 문서를 참고하세요.
이제 다음 명령어로 데이터베이스를 마이그레이션하세요:
rake db:migrate
여기서 테스트 데이터를 추가해봅니다:
- Rails 콘솔을 엽니다:
rails c
- 몇 개의 작업과 링크를 추가합니다:
Task.create :text=>"Task 1", :start_date=>"2015-10-25", :duration=>2, :progress=>0;
Task.create :text=>"Task 2", :start_date=>"2015-10-27", :duration=>3, :progress=>0.5;
Link.create :source=>1, :target=>2, :link_type=>"0";
- "exit"을 입력해 콘솔을 종료합니다.
다음은 컨트롤러에서 데이터 로딩 및 저장 기능을 구현합니다.
4단계. 데이터 로딩하기
모델과 마이그레이션이 준비되었으니, 데이터베이스의 데이터를 Gantt 차트에 로드할 수 있습니다.
dhtmlxGantt는 JSON 형식의 데이터를 기대하므로, GanttController에 데이터를 읽고 포맷하여 출력하는 새 액션을 추가합니다:
app/controllers/gantt_controller.rb
class GanttController < ApplicationController
def index
end
def data
tasks = Task.all
links = Link.all
render :json=>{
:data => tasks.map{|task|{
:id => task.id,
:text => task.text,
:start_date => task.start_date.to_formatted_s(:db),
:duration => task.duration,
:progress => task.progress,
:parent => task.parent,
:open => true
}},
:links => links.map{|link|{
:id => link.id,
:source => link.source,
:target => link.target,
:type => link.link_type
}}
}
end
end
이 액션을 위한 라우트를 routes.rb에 추가하세요:
config/routes.rb
Rails.application.routes.draw do
root :to => "gantt#index"
scope '/api' do/*!*/
get "/data", :to => "gantt#data"/*!*/
end/*!*/
end
클라이언트 측에서는 gantt.load 메서드를 사용해 이 액션을 호출합니다:
app/views/gantt/index.html.erb
gantt.config.date_format = "%Y-%m-%d %H:%i:%s";/*!*/
gantt.init("gantt_here");
gantt.load("/api/data");/*!*/
date_format 설정은 서버에서 받아오는 날짜(예: Task의 start_date)의 포맷을 지정하며, 이는 Rails의 날짜 포맷과 일치해야 합니다.
서버를 시작하고 http://localhost:3000/ 을 열면, 데이터베이스에 저장된 작업과 링크가 Gantt 차트에 표시됩니다. 하지만 아직 변경 사항을 저장할 수는 없습니다. 다음 단계에서 이를 처리합니다.
5단계. 변경사항 저장하기
dhtmlxGantt는 사용자의 모든 변경 사항을 백엔드의 RESTful API로 전송하여 데이터베이스에 저장할 수 있습니다. 이 프로토콜의 자세한 내용은 여기에서 확인할 수 있습니다.
저장 기능을 활성화하려면, 먼저 클라이언트에서 변경 사항 전송을 활성화하세요:
app/views/gantt/index.html.erb
gantt.config.date_format = "%Y-%m-%d %H:%i:%s";
gantt.init("gantt_here");
gantt.load("/api/data");
var dp = new gantt.dataProcessor("/api");/*!*/
dp.init(gantt);/*!*/
dp.setTransactionMode("REST");/*!*/
다음으로, Tasks와 Links 각각에 대해 컨트롤러를 추가하고 필요한 액션을 구현합니다.