{"id":3255,"date":"2016-03-23T16:00:23","date_gmt":"2016-03-23T12:00:23","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=3255"},"modified":"2025-05-19T14:45:55","modified_gmt":"2025-05-19T11:45:55","slug":"using-dhtmlxgantt-with-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/using-dhtmlxgantt-with-ruby-on-rails\/","title":{"rendered":"Using dhtmlxGantt with Ruby on Rails [Tutorial]"},"content":{"rendered":"<p>There&#8217;s no limit to perfection. And no limit to our intentions of providing you with different possible ways of combining our components with your favorite development tools. For example, you can check <a href=\"https:\/\/dhtmlx.com\/blog\/using-dhtmlxgantt-with-laravel\/\" target=\"_blank\">this tutorial<\/a> that describes how you can use our <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGantt\/\" target=\"_blank\">dhtmlxGantt<\/a> along with the <a href=\"https:\/\/laravel.com\/\" target=\"_blank\">Laravel framework<\/a>.<\/p>\n<p>This time, we&#8217;ll go a little bit further and offer you to add a new skill to your knowledge baggage. Let us introduce to you this step-by-step tutorial that covers the using of dhtmlxGantt along with <a href=\"https:\/\/rubyonrails.org\/\" target=\"_blank\">Ruby on Rails<\/a>.<\/p>\n<p><a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGantt\/download.shtml\" onclick=\"ga('send', 'event', 'link', 'click', 'download-gantt-RubyonRails-blog')\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/08\/download-gantt-banner.png\" alt=\"download gantt\" width=\"900\" height=\"300\" class=\"aligncenter size-full wp-image-3693\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/08\/download-gantt-banner.png 900w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/08\/download-gantt-banner-300x100.png 300w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/08\/download-gantt-banner-768x256.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<h3>Creating a New Project<\/h3>\n<p><\/br><br \/>\nIf you don&#8217;t have <a href=\"https:\/\/codelabs.greycampus.com\/\" rel=\"noopener\" target=\"_blank\">Ruby<\/a> on Rails installed, you can follow this <a href=\"https:\/\/guides.rubyonrails.org\/getting_started.html\" target=\"_blank\">installation guide<\/a>. Installation method may difer according to the operating system you use, but it shouldn&#8217;t take long. After you finish installing Rails, you&#8217;ll be able to create a new project.<\/p>\n<p>To do so, type in your terminal:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails <span class=\"kw5\">new<\/span> path<span class=\"sy0\">\/<\/span>to<span class=\"sy0\">\/<\/span>your<span class=\"sy0\">\/<\/span>project<\/div><\/div>\n<p>Now, your first Ruby on Rails project is officially created. Before testing the application, let&#8217;s deal with creating a controller and routing.<\/p>\n<h3>Controllers and Routes<\/h3>\n<p><strong><br \/>\nController&#8217;s<\/strong> primary task is to receive requests that users send to an application. To define which controller will receive a particular request, you use <strong>routing<\/strong>. Usually, applications have several routes and controllers. Different routes can be served by different <strong>actions<\/strong>. Actions collect the information that will be provided to a <strong>view<\/strong>.<\/p>\n<p>Let&#8217;s create a new controller named home. Besides the controller, we&#8217;ll create a new action named index.<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">cd path<span class=\"sy0\">\/<\/span>to<span class=\"sy0\">\/<\/span>your<span class=\"sy0\">\/<\/span>project<br \/>\nrails generate controller home index<\/div><\/div>\n<p>The output should confirm that new files were created. Now, let&#8217;s config the routing. Open the file <em>config\/routes.rb<\/em> and find the following line at the very beginning of this file:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">get <span class=\"st0\">'home\/index'<\/span><\/div><\/div>\n<p>Then, replace it with this code:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">root <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">'home#index'<\/span><\/div><\/div>\n<p>Now we can test our server. Run in your terminal:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails server<\/div><\/div>\n<p>Open in your browser <em>http:\/\/localhost:3000\/<\/em>, and you&#8217;ll get this result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/localhost.png\" alt=\"run rails\" width=\"600\" height=\"400\" class=\"aligncenter size-full wp-image-3263\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/localhost.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/localhost-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Doesn&#8217;t look so impressive at this step, but everything works fine which is a good sign.<\/p>\n<p>Let&#8217;s proceed with the views.<\/p>\n<h3>Unpacking dhtmlxGantt and Creating the Views<\/h3>\n<p><\/br><br \/>\nA <strong>view&#8217;s<\/strong> work is to display the information that was previously collected by an action in a human readable format. Before creating a new view, we need to <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGantt\/download.shtml\" target=\"_blank\">download the dhtmlxGantt package<\/a>.<\/p>\n<p>The vendor directory of your project contains third-party code. This is where we&#8217;ll unpack the dhtmlxGantt files. It will take two steps:<\/p>\n<p>1. Unpack the file <em>codebase\/dhtmlxgantt.js<\/em> and the directory <em>codebase\/ext<\/em> from the package to the directory <em>vendor\/assets\/javascripts\/<\/em> of your project<br \/>\n2. Unpack the file <em>codebase\/dhtmlxgantt.css<\/em> and the directory <em>codebase\/skins<\/em> from the package to the directory <em>vendor\/assets\/stylesheets\/<\/em> of your project<\/p>\n<p>Now, we should add this new files to the precompile array. Open the <em>config\/initializers\/assets.rb<\/em> file and add the following code:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">Rails.<span class=\"me1\">application<\/span>.<span class=\"me1\">config<\/span>.<span class=\"me1\">assets<\/span>.<span class=\"me1\">precompile<\/span> <span class=\"sy0\">+<\/span>= <span class=\"sy0\">%<\/span>w<span class=\"br0\">&#40;<\/span> dhtmlxgantt.<span class=\"me1\">css<\/span> <span class=\"br0\">&#41;<\/span><br \/>\nRails.<span class=\"me1\">application<\/span>.<span class=\"me1\">config<\/span>.<span class=\"me1\">assets<\/span>.<span class=\"me1\">precompile<\/span> <span class=\"sy0\">+<\/span>= <span class=\"sy0\">%<\/span>w<span class=\"br0\">&#40;<\/span> dhtmlxgantt.<span class=\"me1\">js<\/span> <span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>Let&#8217;s now create a view. If there&#8217;s no controller-specific layout, Rails will use the <em>app\/views\/layouts\/application.html.erb<\/em>. This file is a template for all pages that have common elements. We&#8217;ll use it to include dhtmlxgantt.js and dhtmlxgantt.js to our project. Open this file and replace the existing code with the following:<\/p>\n<div class=\"codecolorer-container html4strict default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"html4strict codecolorer\"><span class=\"sc0\">&lt;!DOCTYPE html&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"kw2\">html<\/span>&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"kw2\">head<\/span>&gt;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;<span class=\"kw2\">title<\/span>&gt;<\/span>Gantt<span class=\"sc2\">&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">title<\/span>&gt;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> stylesheet_link_tag &nbsp; &nbsp;<span class=\"st0\">'application'<\/span>, <span class=\"kw3\">media<\/span>: <span class=\"st0\">'all'<\/span>, <span class=\"st0\">'data-turbolinks-track'<\/span> <span class=\"sy0\">=<\/span>&gt;<\/span> true %&gt;<br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> stylesheet_link_tag <span class=\"st0\">'dhtmlxgantt'<\/span>, <span class=\"kw3\">media<\/span>: <span class=\"st0\">'all'<\/span>, <span class=\"st0\">'data-turbolinks-track'<\/span> <span class=\"sy0\">=<\/span>&gt;<\/span> true %&gt;<br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">'application'<\/span>, <span class=\"st0\">'data-turbolinks-track'<\/span> <span class=\"sy0\">=<\/span>&gt;<\/span> true %&gt; &nbsp;<br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">'dhtmlxgantt'<\/span>, <span class=\"st0\">'data-turbolinks-track'<\/span> <span class=\"sy0\">=<\/span>&gt;<\/span> true %&gt;<br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> csrf_meta_tags %&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">head<\/span>&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"kw2\">body<\/span>&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> yield %&gt;<\/span><br \/>\n<br \/>\n<span class=\"sc2\">&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">body<\/span>&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">html<\/span>&gt;<\/span><\/div><\/div>\n<p>Now we can define a view for the home controller that we have created earlier. Open the file <em>app\/views\/home\/index.html.erb<\/em> and add the DIV container for the Gantt chart along with the code that initializes the chart:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"sy0\">&lt;<\/span>div id<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;gantt_here&quot;<\/span> style<span class=\"sy0\">=<\/span><span class=\"st0\">'width:100%; height:800px;'<\/span><span class=\"sy0\">&gt;&lt;\/<\/span>div<span class=\"sy0\">&gt;<\/span><br \/>\n<br \/>\n<span class=\"sy0\">&lt;<\/span>script<span class=\"sy0\">&gt;<\/span><br \/>\n&nbsp; &nbsp; gantt.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;gantt_here&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"sy0\">&lt;\/<\/span>script<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>We can check the changes that we&#8217;ve made. If you&#8217;ve stopped the server you can run it again (rails server), and here&#8217;s what you&#8217;ll see in your browser:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/gantt-chart.png\" alt=\"gantt chart\" width=\"600\" height=\"400\" class=\"aligncenter size-full wp-image-3264\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/gantt-chart.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/gantt-chart-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>It&#8217;s already something. There&#8217;s a Gantt chart that allows you create and change new tasks. But at the moment, your application can&#8217;t save the changes. Let&#8217;s fix it.<\/p>\n<h3>Creating the Models<\/h3>\n<p><\/br><br \/>\nOur application requires two models: one for the tasks and one for the links between them.<\/p>\n<p>Let&#8217;s create a model for the tasks first. Run in your terminal:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails generate model Task text:string start_date:datetime duration:integer progress:float sortorder:integer parent:integer<\/div><\/div>\n<p>As you noticed an ordinary task consists of its name, start date, duration, etc.<\/p>\n<p>Now we can create a second model. For the links this time:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails generate model Link source:integer target:integer link_type:string:limit1<\/div><\/div>\n<p>And, finally, we can create a new database:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rake db:migrate<\/div><\/div>\n<p>Optionally, you can add some tasks and links to your database. Open the Rails console:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails c<\/div><\/div>\n<p>And then, add the new tasks and links. For example:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">Task.<span class=\"me1\">create<\/span> <span class=\"re3\">:text<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"st0\">&quot;Task 1&quot;<\/span>, <span class=\"re3\">:start_date<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"st0\">&quot;2015-10-25&quot;<\/span>, &nbsp;<span class=\"re3\">:duration<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"nu0\">2<\/span>;<br \/>\nTask.<span class=\"me1\">create<\/span> <span class=\"re3\">:text<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"st0\">&quot;Task 2&quot;<\/span>, <span class=\"re3\">:start_date<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"st0\">&quot;2015-10-27&quot;<\/span>, &nbsp;<span class=\"re3\">:duration<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"nu0\">3<\/span>;<br \/>\nLink.<span class=\"me1\">create<\/span> <span class=\"re3\">:source<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"nu0\">1<\/span>, <span class=\"re3\">:target<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"nu0\">2<\/span>, <span class=\"re3\">:link_type<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"st0\">&quot;0&quot;<\/span>;<\/div><\/div>\n<p>When you finish, just type exit to close the Rails console.<\/p>\n<p>Now we should add the code that allows us to save and load the data.<\/p>\n<h3>Creating Controllers to Work with the Data<\/h3>\n<p><\/br><br \/>\nLet&#8217;s create a new action for loading the data. Open the <em>app\/controllers\/home_controller.rb<\/em> file and add the following code that creates the data action:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\"><span class=\"kw1\">def<\/span> data<br \/>\n&nbsp; &nbsp; tasks = Task.<span class=\"me1\">all<\/span><br \/>\n&nbsp; &nbsp; links = Link.<span class=\"me1\">all<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span><span class=\"sy0\">=&gt;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:data<\/span> <span class=\"sy0\">=&gt;<\/span> tasks.<span class=\"me1\">map<\/span><span class=\"br0\">&#123;<\/span><span class=\"sy0\">|<\/span>task<span class=\"sy0\">|<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:id<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">id<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:text<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">text<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:start_date<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">start_date<\/span>.<span class=\"me1\">to_formatted_s<\/span><span class=\"br0\">&#40;<\/span><span class=\"re3\">:db<\/span><span class=\"br0\">&#41;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:duration<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">duration<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:progress<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">progress<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:sortorder<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">sortorder<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:parent<\/span> <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">parent<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#125;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:links<\/span> <span class=\"sy0\">=&gt;<\/span> links.<span class=\"me1\">map<\/span><span class=\"br0\">&#123;<\/span><span class=\"sy0\">|<\/span>link<span class=\"sy0\">|<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:id<\/span> <span class=\"sy0\">=&gt;<\/span> link.<span class=\"me1\">id<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:source<\/span> <span class=\"sy0\">=&gt;<\/span> link.<span class=\"me1\">source<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:target<\/span> <span class=\"sy0\">=&gt;<\/span> link.<span class=\"me1\">target<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re3\">:type<\/span> <span class=\"sy0\">=&gt;<\/span> link.<span class=\"me1\">link_type<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><\/div><\/div>\n<p>Now we should create a new controller task:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails generate controller task<\/div><\/div>\n<p>And add new actions for adding, changing, and deleting the data. To do so, open the <em>app\/controllers\/task_controller.rb<\/em> and add:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">protect_from_forgery<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> <span class=\"kw5\">update<\/span><br \/>\n&nbsp; &nbsp; task = Task.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;id&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"me1\">text<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;text&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"me1\">start_date<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;start_date&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"me1\">duration<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;duration&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"me1\">progress<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;progress&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"me1\">sortorder<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;sortorder&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"me1\">parent<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;parent&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; task.<span class=\"kw5\">save<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#123;<\/span>:status <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;ok&quot;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> <span class=\"kw5\">add<\/span><br \/>\n&nbsp; &nbsp; task = Task.<span class=\"me1\">create<\/span> <span class=\"re3\">:text<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;text&quot;<\/span><span class=\"br0\">&#93;<\/span>, <span class=\"re3\">:start_date<\/span><span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;start_date&quot;<\/span><span class=\"br0\">&#93;<\/span>, <span class=\"re3\">:duration<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;duration&quot;<\/span><span class=\"br0\">&#93;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re3\">:progress<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;progress&quot;<\/span><span class=\"br0\">&#93;<\/span>, <span class=\"re3\">:sortorder<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;sortorder&quot;<\/span><span class=\"br0\">&#93;<\/span>, <span class=\"re3\">:parent<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;parent&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#123;<\/span>:tid <span class=\"sy0\">=&gt;<\/span> task.<span class=\"me1\">id<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> <span class=\"kw5\">delete<\/span><br \/>\n&nbsp; &nbsp; Task.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;id&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"kw5\">destroy<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#123;<\/span>:status <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;ok&quot;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><\/div><\/div>\n<p>As you may have guessed, now it&#8217;s time to create the controller named link. Creating a new controller:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">rails generate controller link<\/div><\/div>\n<p>And don&#8217;t forget to add the code required for adding, changing, and deleting. Open the <em>app\/controllers\/link_controller.rb<\/em> file and add:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">protect_from_forgery<br \/>\n<br \/>\n&nbsp;<span class=\"kw1\">def<\/span> <span class=\"kw5\">update<\/span><br \/>\n&nbsp; &nbsp;link = Link.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;id&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp;link.<span class=\"me1\">source<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;source&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp;link.<span class=\"me1\">target<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;target&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp;link.<span class=\"me1\">link_type<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;type&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp;link.<span class=\"kw5\">save<\/span><br \/>\n<br \/>\n&nbsp; &nbsp;<span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#123;<\/span>:status <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;ok&quot;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp;<span class=\"kw1\">end<\/span><br \/>\n<br \/>\n&nbsp;<span class=\"kw1\">def<\/span> <span class=\"kw5\">add<\/span><br \/>\n&nbsp; &nbsp;link = Link.<span class=\"me1\">create<\/span> <span class=\"re3\">:source<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;source&quot;<\/span><span class=\"br0\">&#93;<\/span>, <span class=\"re3\">:target<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;target&quot;<\/span><span class=\"br0\">&#93;<\/span>, <span class=\"re3\">:link_type<\/span> <span class=\"sy0\">=&gt;<\/span> params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;type&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#123;<\/span>:tid <span class=\"sy0\">=&gt;<\/span> link.<span class=\"me1\">id<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp;<span class=\"kw1\">end<\/span><br \/>\n<br \/>\n&nbsp;<span class=\"kw1\">def<\/span> <span class=\"kw5\">delete<\/span><br \/>\n&nbsp; &nbsp;Link.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;id&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"kw5\">destroy<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw5\">render<\/span> <span class=\"re3\">:json<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#123;<\/span>:status <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;ok&quot;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n&nbsp;<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>Now we need to define the routes to our new controllers and actions. Open the <em>config\/routes.rb<\/em> file and add:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">match <span class=\"st0\">&quot;home\/data&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;home#data&quot;<\/span>, <span class=\"re3\">:as<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;data&quot;<\/span>, <span class=\"re3\">:via<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;get&quot;<\/span><br \/>\n<br \/>\npost <span class=\"st0\">&quot;home\/data\/task&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;task#add&quot;<\/span><br \/>\nput <span class=\"st0\">&quot;home\/data\/task\/:id&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;task#update&quot;<\/span><br \/>\n<span class=\"kw5\">delete<\/span> <span class=\"st0\">&quot;home\/data\/task\/:id&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;task#delete&quot;<\/span><br \/>\n<br \/>\npost <span class=\"st0\">&quot;home\/data\/link&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;link#add&quot;<\/span><br \/>\nput <span class=\"st0\">&quot;home\/data\/link\/:id&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;link#update&quot;<\/span><br \/>\n<span class=\"kw5\">delete<\/span> <span class=\"st0\">&quot;home\/data\/link\/:id&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;link#delete&quot;<\/span><\/div><\/div>\n<p>We&#8217;re almost there. One more step. Open the <em>app\/views\/home\/index.html.erb<\/em> file and add the code that initializes the chart, loads, and saves the data. Insert it between the &#60;script&#62;&#60;\/script&#62; tags:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">gantt.<span class=\"me1\">config<\/span>.<span class=\"me1\">xml_date<\/span>=<span class=\"st0\">&quot;%Y-%m-%d %H:%i&quot;<\/span>;<br \/>\n<br \/>\ngantt.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;gantt_here&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\ngantt.<span class=\"kw3\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= data_path %&gt;&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\nvar dp = <span class=\"kw5\">new<\/span> dataProcessor<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= data_path %&gt;&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\ndp.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span>gantt<span class=\"br0\">&#41;<\/span>;<br \/>\ndp.<span class=\"me1\">setTransactionMode<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;REST&quot;<\/span><span class=\"br0\">&#41;<\/span>;<\/div><\/div>\n<p>At least, we can check the final result. If you open your application in browser, you&#8217;ll see two tasks and a link between them. These are the tasks that we&#8217;ve created earlier.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/gantt-chart-with-rails.png\" alt=\"gantt chart with rails\" width=\"600\" height=\"385\" class=\"aligncenter size-full wp-image-3262\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/gantt-chart-with-rails.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2016\/03\/gantt-chart-with-rails-300x193.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>You can create new tasks and links and change them. The application will save the changes to the database.<\/p>\n<p>Hopefully, this article will be useful to you. Here you can also learn how to use <a href=\"https:\/\/dhtmlx.com\/blog\/use-dhtmlxscheduler-ruby-rails-part-1-tutorial\/\" target=\"_blank\">dhtmlxScheduler with Ruby on Rails<\/a>. Do not forget to visit our blog from time to time. There will be more helpful tutorials for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s no limit to perfection. And no limit to our intentions of providing you with different possible ways of combining our components with your favorite development tools. For example, &#8230;<\/p>\n","protected":false},"author":2,"featured_media":3804,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,71,1,38],"tags":[33,20,14,60,36],"class_list":["post-3255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-posts-for-codeproject-com","category-tutorials","category-uncategorized","category-visible","tag-all-posts","tag-gantt","tag-tips","tag-integrations-gantt","tag-ruby-on-rails"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/3255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/comments?post=3255"}],"version-history":[{"count":6,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/3255\/revisions"}],"predecessor-version":[{"id":13922,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/3255\/revisions\/13922"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media\/3804"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=3255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=3255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=3255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}