{"id":2141,"date":"2014-02-07T10:34:10","date_gmt":"2014-02-07T07:34:10","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=2141"},"modified":"2026-01-26T13:22:30","modified_gmt":"2026-01-26T10:22:30","slug":"dhtmlxgrid-with-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/dhtmlxgrid-with-ruby-on-rails\/","title":{"rendered":"Using DHTMLX Grid with Ruby on Rails"},"content":{"rendered":"<p>This is the update for the <a href=\"https:\/\/dhtmlx.com\/blog\/editable-datagrid-for-ruby-on-rails-built-with-dhtmlxgrid\/\">previous tutorial<\/a> that explains how to use <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGrid\/\" title=\"dhtmlxGrid homepage\">dhtmlxGrid<\/a> with Ruby on Rails backend. We have rewritten this tutorial to be up to date with the latest RoR version. By following these step-by-step instructions, you will get an editable grid that loads its data from Ruby on Rails and saves back changes made by end-users online.<\/p>\n<p><a href=\"https:\/\/dhtmlx.com\/x\/download\/regular\/tutorials\/grid_on_rails.zip\" title=\"Download Ruby on Rails Grid Demo\">Download and check out the final demo<\/a> to see how it is done or follow the instructions below. <\/p>\n<p><a href=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/02\/grid-with-ruby.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/02\/grid-with-ruby.png\" alt=\"dhtmlxGrid Demo with Ruby on Rails\" width=\"549\" height=\"272\" class=\"aligncenter size-full wp-image-2190\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/02\/grid-with-ruby.png 549w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/02\/grid-with-ruby-300x148.png 300w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><\/a><\/p>\n<h3>Getting Started<\/h3>\n<p>&nbsp;<br \/>\nTo get started, create a new Ruby application that will include dhtmlxGrid. If you are using Linux, be sure to install Node.js, it is the mandatory dependency for Rails 4.x. <\/p>\n<p>To begin with, run the command:<\/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> grid_on_rails<\/div><\/div>\n<p>Then go to the created directory <strong>grid_on_rails<\/strong> and generate the controller &#8220;<em>admin<\/em>&#8221; by running the following command:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">$ rails generate controller admin index<\/div><\/div>\n<p>After running this command, Rails generates a set of files required for our application.<\/p>\n<p>Now we need to change the line in the file <em>config\/routes.rb<\/em>:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">get <span class=\"st0\">'admin\/index'<\/span><\/div><\/div>\n<p>to:<\/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\">'admin#index'<\/span><\/div><\/div>\n<p>Thus, we make the page <em>&#8220;index&#8221;<\/em> the main page of the app.\t<\/p>\n<p>Let&#8217;s check if the page <em>&#8220;index&#8221;<\/em> is rendered. Before starting the server, delete the file <em>public\/index.html<\/em>. It is necessary for displaying the needed page and not the home page that was automatically generated while creating the app. <\/p>\n<p>To start the server, run the command:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">&nbsp; &nbsp; &nbsp; &nbsp; $ rails server<\/div><\/div>\n<p><!--more-->Then open your browser and go to the page <em>http:\/\/localhost:3000\/<\/em>. Here, you should see the following:<\/p>\n<p>&nbsp;<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-1.jpg\" alt=\"dhtmlxGrid with Ruby - Demo Page\" width=\"459\" height=\"267\" class=\"aligncenter size-full wp-image-2163\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-1.jpg 459w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-1-300x174.jpg 300w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/p>\n<p>This is the result of rendering the file <em>app\/views\/admin\/index.html.erb<\/em>.  <\/p>\n<h3>Rendering Grid on a Page<\/h3>\n<p>&nbsp;<br \/>\nThe next step is to render the grid on the page we&#8217;ve created. First, <a href=\"https:\/\/dhtmlx.com\/x\/download\/grid\/grid_gpl.zip\">download the dhtmlxGrid package<\/a> (Standard Edition), unzip it, and copy the following files into the directory <strong>vendor\/assets\/javascripts\/<\/strong>: <\/p>\n<ul>\n<li><em>dhtmlxGrid\/codebase\/dhtmlxcommon.js<\/em><\/li>\n<li><em>dhtmlxGrid\/codebase\/dhtmlxgrid.js<\/em><\/li>\n<li><em>dhtmlxGrid\/codebase\/dhtmlxgridcell.js<\/em><\/li>\n<li><em>dhtmlxDataProcessor\/codebase\/dhtmlxdataprocessor.js<\/em><\/li>\n<li>the directory <em>&#8220;ext&#8221;<\/em> from <strong>dhtmlxGrid\/codebase\/<\/strong>\n<\/ul>\n<p>&nbsp;<br \/>\nTo define the styles for the grid, copy the following files into the directory <strong>vendor\/assets\/stylesheets\/<\/strong>:<\/p>\n<ul>\n<li><em>dhtmlxGrid\/codebase\/dhtmlxgrid_skins.css<\/em><\/li>\n<li><em>dhtmlxGrid\/codebase\/dhtmlxgrid.css<\/em><\/li>\n<li>the directory <em>&#8220;skins&#8221;<\/em> from <strong>dhtmlxGrid\/codebase\/<\/strong> <\/li>\n<\/ul>\n<p>&nbsp;<br \/>\nThe directory <strong>vendor\/assets<\/strong> is intended for placing third-party files, such as CSS, JavaScript files, and images.<\/p>\n<p>In the directory <em>&#8220;public&#8221;<\/em>, create a directory with the name <em>&#8220;assets&#8221;<\/em> and copy the folder <em>&#8220;imgs&#8221;<\/em> from <strong>dhtmlxGrid\/codebase\/<\/strong> into it.<\/p>\n<p>Open the file <em>app\/views\/layouts\/application.html.erb<\/em> and edit it to look like this:<\/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>GridOfRails<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 <span class=\"st0\">&quot;dhtmlxgrid&quot;<\/span>, :<span class=\"kw3\">media<\/span> <span class=\"sy0\">=<\/span>&gt;<\/span> &quot;all&quot; %&gt;<br \/>\n&nbsp; &nbsp;<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> stylesheet_link_tag <span class=\"st0\">&quot;skins\/dhtmlxgrid_dhx_skyblue&quot;<\/span>, :<span class=\"kw3\">media<\/span><span class=\"sy0\">=<\/span>&gt;<\/span> &quot;all&quot; %&gt;<br \/>\n&nbsp; &nbsp;<br \/>\n&nbsp; &nbsp;<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">&quot;dhtmlxcommon&quot;<\/span> %&gt;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">&quot;dhtmlxgrid&quot;<\/span> %&gt;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">&quot;dhtmlxgridcell&quot;<\/span> %&gt;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">&quot;dhtmlxdataprocessor&quot;<\/span> %&gt;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> javascript_include_tag <span class=\"st0\">&quot;ext\/dhtmlxgrid_filter&quot;<\/span> %&gt;<\/span><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<span class=\"sc2\">&lt;%<span class=\"sy0\">=<\/span> yield %&gt;<\/span><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>This file is a template for all the pages of our app where common elements are placed. <\/p>\n<p>The next step is to change the file <em>app\/views\/admin\/index.html.erb<\/em> the following way:<\/p>\n<div class=\"codecolorer-container html4strict default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"html4strict codecolorer\">&lt;<span class=\"kw2\">div<\/span> <span class=\"kw3\">id<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;grid_here&quot;<\/span> <span class=\"kw3\">style<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;width: 600px; height: 400px;&quot;<\/span>&gt;&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">div<\/span>&gt;<br \/>\n&lt;<span class=\"kw2\">input<\/span> <span class=\"kw3\">type<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;button&quot;<\/span> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;Add&quot;<\/span> <span class=\"kw3\">onclick<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;grid.addRow(grid.uid(),'new user')&quot;<\/span> <span class=\"sy0\">\/<\/span>&gt;<br \/>\n&lt;<span class=\"kw2\">input<\/span> <span class=\"kw3\">type<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;button&quot;<\/span> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;Delete&quot;<\/span> <br \/>\n<span class=\"kw3\">onclick<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;grid.deleteSelectedRows()&quot;<\/span> <span class=\"sy0\">\/<\/span>&gt;<br \/>\n<br \/>\n&lt;<span class=\"kw2\">script<\/span> <span class=\"kw3\">type<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text\/javascript&quot;<\/span> <span class=\"kw3\">charset<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;UTF-8&quot;<\/span>&gt;<br \/>\n&nbsp; &nbsp; &nbsp;var grid <span class=\"sy0\">=<\/span> new dhtmlXGridObject<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;grid_here&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.setImagePath<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;assets\/imgs\/&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.setHeader<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;First name, Last name, Phone&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.attachHeader<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;#text_filter,#text_filter,#text_filter&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.setColSorting<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;str,str,str&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.setInitWidths<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;100,100,*&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.setSkin<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;dhx_skyblue&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.init<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&lt;<span class=\"sy0\">\/<\/span><span class=\"kw2\">script<\/span>&gt;<\/div><\/div>\n<p>Now, when you start the server and go to the page <em>http:\/\/localhost:3000\/<\/em>, you should see an empty grid with the header:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-2.jpg\" alt=\"Grid with Ruby - Grid on page\" width=\"496\" height=\"339\" class=\"aligncenter size-full wp-image-2164\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-2.jpg 496w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-2-300x205.jpg 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/p>\n<p>&nbsp;<br \/>\nSo, we&#8217;ve got a working app: on clicking the &#8220;Add&#8221; button, a new row will be added, and on selecting a row and clicking the &#8220;Delete&#8221; button, the row will be deleted. <\/p>\n<p>However, our app works only on the client-side at the moment and all the changes will not be saved after the page reload. That&#8217;s why our next steps will be creating a database and synchronizing dhtmlxGrid with it. <\/p>\n<h3>Creating a Database<\/h3>\n<p>&nbsp;<br \/>\nTo create a database, do the following:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">$ rails generate model User first_name:string last_name:string phone:string<\/div><\/div>\n<p>This command will generate a new migration file <em>db\/migrate\/[&#8230;]_create_users.rb<\/em> and a model file <em>app\/models\/user.rb<\/em>. The names of the database table fields and their types are specified in this command after the model&#8217;s name &#8211; &#8220;User&#8221;. Thus, the table will have three text fields: first_name, last_name, phone. <\/p>\n<p>Although we&#8217;ve created the migration file and the model file, the DB itself isn&#8217;t created yet. To create it, run the command:<\/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>Now, as the DB is ready, and we can work with it, run the Rails console and add a record in the DB. For that, run the command:<\/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<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\"><span class=\"sy0\">&gt;<\/span> User.<span class=\"me1\">create<\/span> <span class=\"re3\">:first_name<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;John&quot;<\/span>, <span class=\"re3\">:last_name<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;Smit&quot;<\/span>, <span class=\"re3\">:phone<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;555-33-77&quot;<\/span><\/div><\/div>\n<p>Now, to check if there&#8217;s a record &#8220;John, Smit, 555-33-77&#8221; in the &#8220;<em>users<\/em>&#8221; table, run the command:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\"><span class=\"sy0\">&gt;<\/span> User.<span class=\"me1\">all<\/span><\/div><\/div>\n<p>You will see the following text:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">&nbsp;<span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#91;<\/span><span class=\"co1\">#&lt;User id: 1, first_name: &quot;John&quot;, last_name: &quot;Smit&quot;, phone: &quot;555-33-77&quot;,<\/span><br \/>\ncreated_at: <span class=\"st0\">&quot;2013-06-03 10:34:56&quot;<\/span>, updated_at: <span class=\"st0\">&quot;2013-06-03 10:34:56&quot;<\/span><span class=\"sy0\">&gt;<\/span><span class=\"br0\">&#93;<\/span><\/div><\/div>\n<p>This command will output the list of records stored in the DB table. To escape the console, run the command:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\"><span class=\"sy0\">&gt;<\/span> <span class=\"kw3\">exit<\/span><\/div><\/div>\n<h3>Loading Data in Grid<\/h3>\n<p>&nbsp;<br \/>\nLet&#8217;s realize data loading into dhtmlxGrid. Do the following steps:<\/p>\n<p>Open the file <em>config\/routes.rb<\/em> and add into it the path through which dhtmlxGrid will request the list of records. Add the line:<\/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;admin\/data&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#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><\/div><\/div>\n<p>Like this:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\"><span class=\"re2\">GridOfRails::Application<\/span>.<span class=\"me1\">routes<\/span>.<span class=\"me1\">draw<\/span> <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; &nbsp;root <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#index&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;match <span class=\"st0\">&quot;admin\/data&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#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&nbsp; &nbsp; &nbsp;\u2026<br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>This line contains the following information:<\/p>\n<ul>\n<li>&#8220;admin\/data&#8221; \u2014 url that the app will respond to<\/li>\n<li>:to => &#8220;admin#data&#8221; \u2014 specifies that by request to the above url, the action &#8220;data&#8221; from the controller &#8220;admin&#8221; will be performed<\/li>\n<li>:as => &#8220;data&#8221; \u2014 helpers &#8220;data_path&#8221; and &#8220;data_url&#8221; will be created; they return relative and absolute url, respectively<\/li>\n<li>:via => &#8220;get&#8221; &#8211; type of request<\/li>\n<\/ul>\n<p>&nbsp;<br \/>\nOpen the file <em>app\/views\/admin\/index.html.erb<\/em> and add the line:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">grid.<span class=\"kw3\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= data_path %&gt;&quot;<\/span>, <span class=\"st0\">&quot;json&quot;<\/span><span class=\"br0\">&#41;<\/span>;<\/div><\/div>\n<p>Like this:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">...<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"me1\">grid<\/span>.<span class=\"me1\">setSkin<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;dhx_skyblue&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp;grid.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp;grid.<span class=\"kw3\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= data_path %&gt;&quot;<\/span>, <span class=\"st0\">&quot;json&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"sy0\">&lt;\/<\/span>script<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>This line tells dhtmlxGrid the address to send the data to and informs that the data will be received in JSON format. Instead of  &#8220;<%= data_path %>&#8221; the necessary url will be inserted. <\/p>\n<p>Now create the action &#8220;data&#8221; in the &#8220;admin&#8221; controller. To do it, open the file <em>app\/controllers\/admin_controller.rb<\/em> and add the following code into it:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">&nbsp;<span class=\"kw1\">class<\/span> AdminController <span class=\"sy0\">&lt;<\/span> ApplicationController<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">def<\/span> index<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">end<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">def<\/span> data<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; users = User.<span class=\"me1\">all<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp;<span class=\"re3\">:total_count<\/span> <span class=\"sy0\">=&gt;<\/span> users.<span class=\"kw5\">length<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re3\">:pos<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">0<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re3\">:rows<\/span> <span class=\"sy0\">=&gt;<\/span> users.<span class=\"me1\">map<\/span> <span class=\"kw1\">do<\/span> <span class=\"sy0\">|<\/span>user<span class=\"sy0\">|<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re3\">:id<\/span> <span class=\"sy0\">=&gt;<\/span> user.<span class=\"me1\">id<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re3\">:data<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">&#91;<\/span>user.<span class=\"me1\">first_name<\/span>, user.<span class=\"me1\">last_name<\/span>, user.<span class=\"me1\">phone<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">end<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">end<\/span><br \/>\n&nbsp;<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>The action &#8220;data&#8221; will read the list of records and return it to the client in JSON format.<\/p>\n<p>Let&#8217;s check, if everything works properly. Start the server and open the page <em>http:\/\/localhost:3000\/<\/em>, you should see the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-3.png\" alt=\"Empty Grid on Page - dhtmlxGrid with Ruby on Rails\" width=\"500\" height=\"403\" class=\"aligncenter size-full wp-image-2177\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-3.png 500w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/01\/ruby-grid-3-300x241.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>While loading the page, dhtmlxGrid renders the line &#8220;John, Smit, 555-33-77&#8221;. Now the app can only read data from DB. As for adding, deleting and editing the records in DB, these functions aren&#8217;t realized yet. Let&#8217;s add this functionality.  <\/p>\n<h3>Adding, Deleting and Editing Records in DB<\/h3>\n<p>&nbsp;<br \/>\nFirst, we define the URL where dhtmlxGrid will send queries to make changes in DB. <\/p>\n<p>Open the file <em>config\/routes.rb<\/em> and add the line:<\/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;admin\/db_action&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#db_action&quot;<\/span>, <span class=\"re3\">:as<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;db_action&quot;<\/span>, <span class=\"re3\">:via<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;get&quot;<\/span><\/div><\/div>\n<p>Like this:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\"><span class=\"re2\">GridOfRails::Application<\/span>.<span class=\"me1\">routes<\/span>.<span class=\"me1\">draw<\/span> doing<br \/>\n&nbsp; &nbsp; root <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#index&quot;<\/span><br \/>\n&nbsp; &nbsp; match <span class=\"st0\">&quot;admin\/data&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#data&quot;<\/span>, <span class=\"re3\">:as<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;data&quot;<\/span><br \/>\n&nbsp; &nbsp; match <span class=\"st0\">&quot;admin\/db_action&quot;<\/span>, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;admin#db_action&quot;<\/span>, <span class=\"re3\">:as<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;db_action&quot;<\/span>, <span class=\"re3\">:via<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">&quot;get&quot;<\/span><br \/>\n...<br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>Open the file <em>app\/views\/admin\/index.html.erb<\/em> and add into it the line:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">dp = <span class=\"kw5\">new<\/span> dataProcessor<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= db_action_path %&gt;&quot;<\/span><span class=\"br0\">&#41;<\/span>;<span class=\"st0\">&quot; \u0438 &quot;<\/span>dp.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span>grid<span class=\"br0\">&#41;<\/span>;<\/div><\/div>\n<p>Like this:<\/p>\n<div class=\"codecolorer-container rails default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"rails codecolorer\">...<br \/>\n&nbsp; &nbsp; <span class=\"me1\">grid<\/span>.<span class=\"kw3\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= data_path %&gt;&quot;<\/span>, <span class=\"st0\">&quot;json&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; dp = <span class=\"kw5\">new<\/span> dataProcessor<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;&lt;%= db_action_path %&gt;&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; dp.<span class=\"me1\">init<\/span><span class=\"br0\">&#40;<\/span>grid<span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"sy0\">&lt;\/<\/span>script<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>The first line creates the object &#8220;dataProcessor&#8221; and assigns to the variable &#8220;dp&#8221; the link to this object. In place of &#8220;<%= db_action_path %>&#8221; there will be the URL through which dataProcessor will communicate with the server. In our case, it will be &#8220;admin\/db_action&#8221;. The second line attaches dataProcessor to dxtmlxGrid.<\/p>\n<p>Open the file <em>app\/controllers\/admin_controller.rb<\/em> and add the action &#8220;db_action&#8221; into it:<\/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> db_action<br \/>\n&nbsp; &nbsp; <span class=\"re1\">@mode<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;!nativeeditor_status&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; first_name = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;c0&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; last_name = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">'c1'<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; phone = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">'c2'<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re1\">@id<\/span> = params<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;gr_id&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">case<\/span> <span class=\"re1\">@mode<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">when<\/span> <span class=\"st0\">&quot;inserted&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; user = User.<span class=\"me1\">create<\/span> <span class=\"re3\">:first_name<\/span> <span class=\"sy0\">=&gt;<\/span> first_name, <span class=\"re3\">:last_name<\/span> <span class=\"sy0\">=&gt;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; last_name, <span class=\"re3\">:phone<\/span> <span class=\"sy0\">=&gt;<\/span> phone<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">@tid<\/span> = user.<span class=\"me1\">id<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">when<\/span> <span class=\"st0\">&quot;deleted&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; User.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>@id<span class=\"br0\">&#41;<\/span>.<span class=\"kw5\">destroy<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">@tid<\/span> = <span class=\"re1\">@id<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">when<\/span> <span class=\"st0\">&quot;updated&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; user = User.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>@id<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; user.<span class=\"me1\">first_name<\/span> = first_name<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; user.<span class=\"me1\">last_name<\/span> = last_name<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; user.<span class=\"me1\">phone<\/span> = phone<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; user.<span class=\"kw5\">save<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">@tid<\/span> = <span class=\"re1\">@id<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">end<\/span><br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>This action receives data from dataProcessor, processes them and returns the result of view processing &#8220;db_action&#8221;. This view doesn&#8217;t exist yet, you should create it now.  <\/p>\n<p>DataProcessor should receive a response from the server in XML format. Create the file <em>app\/views\/admin\/db_action.xml.erb<\/em> with the following content:<\/p>\n<div class=\"codecolorer-container xml default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"xml codecolorer\"><span class=\"sc3\"><span class=\"re1\">&lt;?xml<\/span> <span class=\"re0\">version<\/span>=<span class=\"st0\">&quot;1.0&quot;<\/span> <span class=\"re0\">encoding<\/span>=<span class=\"st0\">&quot;UTF-8&quot;<\/span><span class=\"re2\">?&gt;<\/span><\/span><br \/>\n<span class=\"sc3\"><span class=\"re1\">&lt;data<span class=\"re2\">&gt;<\/span><\/span><\/span><br \/>\n&nbsp; &nbsp; <span class=\"sc3\"><span class=\"re1\">&lt;action<\/span> <span class=\"re0\">type<\/span>=<span class=\"st0\">&quot;&lt;%= @mode %&gt;<\/span><\/span>&quot; sid=&quot;<span class=\"sc3\">&lt;%= @id %<span class=\"re2\">&gt;<\/span><\/span>&quot; tid=&quot;<span class=\"sc3\">&lt;%= @tid %<span class=\"re2\">&gt;<\/span><\/span>&quot; \/&gt;<br \/>\n<span class=\"sc3\"><span class=\"re1\">&lt;\/data<span class=\"re2\">&gt;<\/span><\/span><\/span><\/div><\/div>\n<p>All the variables in action, starting from &#8220;@&#8221;, are available during view processing. <\/p>\n<p>Then start the server and go to the page <em>http:\/\/localhost:3000\/<\/em>. You can now edit, add, and delete records in the grid. All the changes should automatically synchronize with DB. <\/p>\n<p>Now we have finished our app. We have an editable Ruby on Rails grid that loads its data from the DB and saves all the changes while the end users edit, add, or delete records in the browser. You can <a href=\"https:\/\/dhtmlx.com\/x\/download\/regular\/tutorials\/grid_on_rails.zip\" title=\"Download Ruby on Rails Grid Demo\">download the final grid demo<\/a> and see how it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the update for the previous tutorial that explains how to use dhtmlxGrid with Ruby on Rails backend. We have rewritten this tutorial to be up to date &#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,6,14,36],"class_list":["post-2141","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-dhtmlxgrid","tag-tips","tag-ruby-on-rails"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2141","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=2141"}],"version-history":[{"count":2,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2141\/revisions"}],"predecessor-version":[{"id":14990,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2141\/revisions\/14990"}],"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=2141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=2141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=2141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}