In this tutorial we’d like to show you how to use our event calendar, dhtmlxScheduler, with Ruby on Rails, a web application framework written in Ruby. The tutorial consists of two parts: firstly, we’ll create a simple scheduler with data saving and loading, the second part is adding recurring events to it. Let’s start.
Creating a scheduler
Step 1. The first thing we need to do is to create a new project. For this we need to run the next command:
Step 2. After that create a home controller:
Step 3. Open config/routes.rb and replace
with
When it’s done, check the server performance with the following command:
When you open http://localhost:3000/ in browser, you should get this page:
Step 4. Finished with these settings, we’ll proceed to Scheduler component. So, the next step is to download dhtmlxScheduler.
The following files unpack to vendor/assets/javascripts/:
codebase/dhtmlxscheduler.js
codebase/ext
codebase/locale
These files to vendor/assets/stylesheets/:
codebase/dhtmlxscheduler.css
codebase/dhtmlxscheduler_classic.css
codebase/dhtmlxscheduler_flat.css
codebase/dhtmlxscheduler_glossy.css
Then we need to create “assets” folder in the “public” directory and unpack the following files to it:
codebase/imgs
codebase/imgs_dhx_terrace
codebase/imgs_flat
codebase/imgs_glossy
Open config/initializers/assets.rb. We need to add the style sheet and dhtmlxscheduler.js file to the precompile array.
For this, add the next lines to this file:
Rails.application.config.assets.precompile += %w( dhtmlxscheduler.js )
Step 5. Open app/views/layouts/application.html.erb. This file is a template for all the pages that have common elements.
Here we include dhtmlxscheduler.js and dhtmlxscheduler.css
<html>
<head>
<title>Scheduler on Rails</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'dhtmlxscheduler', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'dhtmlxscheduler', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Step 6. In this step we open app/views/home/index.html.erb, the file that is used for a view for our home controller. It will display the scheduler.
Now we add a container for scheduler, and then initialize our event calendar:
<div class="dhx_cal_navline">
<div class="dhx_cal_prev_button"> </div>
<div class="dhx_cal_next_button"> </div>
<div class="dhx_cal_today_button"></div>
<div class="dhx_cal_date"></div>
<div class="dhx_cal_tab" name="day_tab" style="right:204px;"></div>
<div class="dhx_cal_tab" name="week_tab" style="right:140px;"></div>
<div class="dhx_cal_tab" name="month_tab" style="right:76px;"></div>
</div>
<div class="dhx_cal_header">
</div>
<div class="dhx_cal_data">
</div>
</div>
<script>
scheduler.init("scheduler_here");
</script>
If we run the server now, we should get our scheduler.
Step 7. Well, dhtmlxSheduler is initialized and we may proceed to further settings. Let’s create a model for an event. For this, run the following command:
To create a database, run this:
Step 8. The database is created, but let’s check it and add a record. For this, run:
And add some sample record:
To display all the records, we need to run this command:
For exit we use “exit” command.
Step 9. Now we open config/routes.rb and add the route for data loading:
Step 10. Open app/controllers/home_controller.rb and add “data” action to the controller:
events = Event.all
render :json => events.map {|event| {
:id => event.id,
:start_date => event.start_date.to_formatted_s(:db),
:end_date => event.end_date.to_formatted_s(:db),
:text => event.text
}}
end
Step 11. In this step we open app/views/home/index.html.erb for adding data loading from the server. It’s important to set scheduler.config.xml_date for correct dates parsing.
<div class="dhx_cal_navline">
<div class="dhx_cal_prev_button"> </div>
<div class="dhx_cal_next_button"> </div>
<div class="dhx_cal_today_button"></div>
<div class="dhx_cal_date"></div>
<div class="dhx_cal_tab" name="day_tab" style="right:204px;"></div>
<div class="dhx_cal_tab" name="week_tab" style="right:140px;"></div>
<div class="dhx_cal_tab" name="month_tab" style="right:76px;"></div>
</div>
<div class="dhx_cal_header">
</div>
<div class="dhx_cal_data">
</div>
</div>
<script>
If everything is done correctly, the data loading should be successful.
Step 12. We continue with opening config/routes.rb for adding a route for data handling:
Step 13. Open app/controllers/home_controller.rb. Add “db_action” action to the controller:
mode = params["!nativeeditor_status"]
id = params["id"]
start_date = params["start_date"]
end_date = params["end_date"]
text = params["text"]
case mode
when "inserted"
event = Event.create :start_date => start_date, :end_date => end_date, :text => text
tid = event.id
when "deleted"
Event.find(id).destroy
tid = id
when "updated"
event = Event.find(id)
event.start_date = start_date
event.end_date = end_date
event.text = text
event.save
tid = id
end
render :json => {
:type => mode,
:sid => id,
:tid => tid,
}
end
Step 14. To save the changes in scheduler we need to use DataProcessor. Open app/views/home/index.html.erb.
Add dataProcessor:
dp.init(scheduler);
dp.setTransactionMode("GET", false);
As a result we should have:
<div class="dhx_cal_navline">
<div class="dhx_cal_prev_button"> </div>
<div class="dhx_cal_next_button"> </div>
<div class="dhx_cal_today_button"></div>
<div class="dhx_cal_date"></div>
<div class="dhx_cal_tab" name="day_tab" style="right:204px;"></div>
<div class="dhx_cal_tab" name="week_tab" style="right:140px;"></div>
<div class="dhx_cal_tab" name="month_tab" style="right:76px;"></div>
</div>
<div class="dhx_cal_header">
</div>
<div class="dhx_cal_data">
</div>
</div>
<script>
scheduler.config.xml_date="%Y-%m-%d %H:%i";
scheduler.init("scheduler_here");
scheduler.load("<%= data_path %>", "json");
var dp = new dataProcessor("<%= db_action_path %>");
dp.init(scheduler);
dp.setTransactionMode("GET", false);
</script>
But if you’d like to use POST requests for changing the data in the database, you need to specify Transaction Mode dataProcessor = POST. Moreover, you need to change the corresponding route to:
And you need to open app/controllers/application_controller.rb and set
instead of
Step 15. Finally we’re ready to check our results. So, run the server:
Well, this is everything you need to start using dhtmlxScheduler with Ruby on Rails. The second part of our tutorial that will show you how to set recurring events in dhtmlxScheduler with RoR will be released in a week. Stay tuned!
Comments
Icons doesn’t work on Rails 4.2. Can’t load from assets. Bugs in dhtmlxscheduler.css. Flat file make unavailable to fix.
Hello.
Have you unpacked your images like mentioned in tutorial?
You should get something like following:
…application\public\assets\imgs
…application\public\assets\imgs_dhx_terrace
…application\public\assets\imgs_flat
…application\public\assets\imgs_glossy
I’ve tried on rails 4.2.5.1 and images were loaded correctly.
Hello,
I’ve unpacked all files correctly. I have no idea why it wasn’t work. I had to abandon this for another product.
Regards
Hi Kaz,
Please create a topic on forum: https://forum.dhtmlx.com/
We will do our best to help you.
What is the difference between using “get” or “post”? Whichever I use results in the data getting stored in the database.
Hi Chris,
If you defined a route for a db_action method the same way as it shown in the tutorial, i.e.:
match “home/db_action”, :to => “home#db_action”, :as => “db_action”, :via => “post”
Method that saves changes to the database should be triggered only via POST request – http://guides.rubyonrails.org/routing.html#http-verb-constraints
Hi
Is there a repo for this example?
Thanks
Shaun
Hi Shaun,
Currently, there is no repository, we’ll set it up as soon as we update the getting started/backend integration section of scheduler docs similarly to how it’s done for dhtmlxGantt: https://docs.dhtmlx.com/gantt/desktop__howtostart_guides.html
For now, you can download the demo here:
https://files.dhtmlx.com/30d/213bcfa165ba764fbc7a8db3b13c63a1/dhtmlx-scheduler-rails.zip
And … does the model have to be called ‘Event’, or is the name irrelevant?
Hi Shaun,
Yes, the model name is irrelevant.
Hi Aras
When I create a new record in the database manually, it is showing record on the calendar object, but not any of the additional fields I have created. If I attempt to create a new record in the front end, it throws this error:
undefined method `appartement_name’ for nil:NilClass Extracted source (around line #38):
What I am hoping to happen is that the fields appear on the front end when I create a record manually in the DB, and for that error not to be present.
I have used ‘bookings’ as my model rather than ‘events’ as shown in the tutorial.
Here is my home_controller.rb code (https://gist.github.com/shaunhughston/d3aa191e7c3458a3d79c8790f2025734)
If you could cast your eye over it, it would be much appreciated. Trying to get a demo up for a client so they can approve and they can buy a licence.
Tags
Archives