#213 Calendars (revised)
Learn how to add a calendar to your Rails app. Whether it be a date picker using jQuery UI or a full page calendar for browsing records, I will show you how to do it in this episode.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Fantastic. Timely. I'm working at revising an app we use to schedule assignments in our small photo business. (Internal use, not client-facing.) I'm using FullCalendar for the calendar but I am really getting frustrated by not being able to find a dateTIMEpicker that's as clean and sophisticated as the jQueryUI datepicker. I don't care much for the ones I've seen that use a slider for the hour/minute, and the one I used for the original version of this app is pretty out of date. Anyone have any suggestions? I'm fine with a select widget for hour/minute, but it needs to be integrated within the popup calendar widget. I'm about ready to settle for a simple text entry field (with Chronic) instead, ispired by the Virtual Attribute revised episode from a short while back.
I really love what you have demonstrated about making a calendar from scratch. I think I'll stick with FullCalendar for my project though, because I need event blocks to overlap (to indicate schedule conflicts) and to be able to colorize different types of events (confirmed, tentative, staff appointments...)
Thank you so much, Ryan!
There is an extension to jQuery's datepicker called datetimepicker I believe that adds hours and minutes to the selector. It does use a slider, but works well. Think this is it http://trentrichardson.com/examples/timepicker/
I find this to be the most effective way for entering date and time. EVER
http://keith-wood.name/datetimeEntry.html
I tried these other solutions as well but none is as natural and efficient when you have to pick 100+ of dates and times:
I call this, Normalized units calendar
http://www.ama3.com/anytime/#AnyTime.picker.placement
iPhone Style
http://demo.mobiscroll.com/#demo=datetime&theme=default&display=modal&mode=mixed
Windows System Tray Like
http://www.eyecon.ro/datepicker/
an O.K. one
http://stefangabos.ro/jquery/zebra-datepicker/
TimePicker
https://github.com/fgelinas/timepicker
https://github.com/ChiperSoft/Kalendae
Hope these help
Thank you very much for those suggestions, Nik and Luis. I really appreciate the pointers. I personally really don't find a slider to be a very effective input tool for time. I could live with 3 selects (12-hour, minute, am/pm) or just text entry (like Basecamp's current event entry interface), but integrated into the datepicker. I'll keep looking. The hint from the Kalendae developer that time is in the works is encouraging. I hadn't been aware of that project, so thanks for the link!
Luis's suggestion has many options including removing the slider. Check out all the examples and most likely one of them will fit your needs
Had the same Problem. Try http://malot.fr/bootstrap-datetimepicker/index.php.
It can be easily integrated with the following gem:
https://github.com/smalot/bootstrap-datetimepicker.
Worked well for me.
(If this info is outdated for you, it might help someone else.)
That's not a gem...
Great episode! It would have been nice to see you limit the @articles query to grab articles in the current month as well, but that's not too hard.
Awesome! I have all of the full screen JS calendars I've seen. This is nice and clean and can easily be customized.
Great job.
I agree. I plan to use this on my app. I'll probably check the size and if it's more than 5 items for that day will have a
<%= link_to "#{@model.size} more ...", list_of_models_path(@model) %>
If you are using twitter bootstrap in your app you might find this calendar very handy: http://www.eyecon.ro/bootstrap-datepicker/ It integrates nicely with bootstrap design and seems to have the same functionality as the one from jquery ui.
this gem may me useful for twitter bootstrap users who need a time as well http://jdewit.github.com/bootstrap-timepicker/
Struct.new gives me a superclass mismatch
I fixed this by removing the < Struct.new and adding
attr_accessor :view, :date, :callback
Something is wrong with your configuration then. I downloaded the source and ran the "blog_after." It works perfectly.
get rid of your watu_table_builder gem
This can happen using DRB gems like Spork and Zeus
Also of possible use: I recently had to add a monthpicker to a client's Rails project, allowing the user to select a month and a year, but no day. There's a useful add-on for this functionality here: https://github.com/thebrowser/jquery.ui.monthpicker
Thank you so much for this episode!
I'm also happy with another great jQuery plugin named fullcalendar.
It has Lots of support such as events from JSON and is updated frequently, theck it out! : )
I am using the fullcalendar to generate calendar too.
how do you like it?
Fullcalendar is very, very good. I've used it quite a bit.
How did you expose your events to Fullcalendar? did you pass the json with gon or as an array? I have my events passed with gon sitting in window.gon but I have no idea how to set it into events:[
Maybe I am over complicating this with gon? I can just render it directly with json and use the GET method in Fullcalendar?
Kalendae is a great alternative to including jQuery UI for just a date picker. I wrote a gem to provide as an asset for Rails 3.1+
https://github.com/sdball/kalendae_assets
http://rakeroutes.com/blog/add-kalendae-to-your-rails-app-with-kalendae-assets/
I am used http://www.eyecon.ro/bootstrap-datepicker/ it is verry usable
If your field is a datetime field instead of a date field (the example in this tutorial
:published_on
is a date field), then you can adjust your controller as per this answer on StackOverflow@diasks2 - Thank you so much for this. You just saved me a few hours!
This was a lifesaver! Now I can sleep well tonight! Thank You! Thank You!! Thank You!!!
No one mentioned it, but if you sent type="date" HTML5 compatible browsers will give you a datepicker without any need for javascript.
that was nice :D
i would like to see how u insert a datepicker when u have to change the format to the user, but to save on the database u have to change the format back to rails.
its easy to change the format from / to - only, i really got trapped when i need to format 2012-06-31 to 31/06/2012 to the user and save correctly to the database... thats what i really need to know
Hi -- I think the issue you're facing is that since ruby 1.9.3 Date.parse doesn't handle the ambiguity between dd/mm/yyyy and mm/dd/yyyy.
I dealt with this using a gem called american_date -- since your format is non-american, you may want to use the gem code (short, sweet) as an example.
This ended up working for me as well.
http://stackoverflow.com/questions/14012584/convert-standard-rails-ruby-time-to-american-format-dates-getting-mixed-up
I ran into a similar issue: http://stackoverflow.com/questions/15531786/how-do-i-change-the-format-activerecord-expects-when-parsing-dates-from-a-text-f
This is weird. I have a model called calendar_event. So, in the .coffee file I have
jQuery ->
$('#calendar_event_starts_at').datepicker
dateFormat: 'yy-mm-dd'
which isn't firing datepicker as expected. Does the multi-word model have an effect on jQuery? I've used datepicker before without issue, but all models have been of one word.
Thanks,
Steve
Steve - did you try clicking in the text box? My datepicker doesn't appear until I do that.
I get
ArgumentError: wrong number of arguments (1 for 0)
for this call:first = date.beginning_of_month.beginning_of_week(START_DAY)
If I try it in the console it also gives me the same error.
Date.today.beginning_of_month.beginning_of_week(:sunday)
That feature was released with active support 3.2. Try to update it!
Awesome. Why don't you wrap your calendar helpers into gem?
Any recommendations for generating an .ics file for the date selected? The ri_cal library on github appears to be getting no love. And from the examples, I am not quite sure where to put the code (this video suggests creating a helper).
Any assistance is very appreciated.
Likewise, any advice on this front would be warmly received.
Great episode as always...
i implement this episode to my event model. Some events have a duration of more than 1 day...by example event "A" is 3 day's. Has anybody implement this feature yet. So with 2 columns in the DB 'starts_at' and 'ends_at' and the calendar is filled in between this data?
Ciao..remco
Great episode. I've followed it to the letter, and the datepicker appears, but the options to the datepicker aren't taking effect. I've looked carefully at ordering, and the output javascript file which looks great with options present. I can repost with more code if desired. I am also using twitter-bootstrap-rails and jquery-fileupload-rails gems and wonder if those might be causing conflicts. This should just work. Thanks!
Is there a recommended way to add events to a Google Calendar account?
once again very useful Ryan, thank you!
For what it's worth, if you are using the refinery-calendar with refinerycms and want to embed a calendar grid, the month-to-month navigation like this won't work, and will throw a routing error when you load the page:
=link_to "<", date: @date.prev_month
Instead, you'll need
=link_to "<", refinery.url_for(date: @date.prev_month)
which works just fine. Something of a quirk in Rails' mountable engines routing.
—Steve
omg thanks allot this saved me so much time.
Ryan -- you briefly mentioned the default format of datepicker was mm/dd/yyyy, and changed to something ruby can parse, like yyyy-mm-dd. I wanted the date format displayed as an American date in forms (and elsewhere).
I am sure I have an inelegant solution, so any recommendations are welcome.
To handle date parsing, I used the https://github.com/jeremyevans/ruby-american_date gem, which make Date.parse work as it did in ruby 1.9.2, accepting the mm/dd/yyyy format.
For the form field, I needed to handle both edit and create:
The
date_formatted
method is a helper inapplication_controller.rb
like thisand the JS is done (definitely inelegantly) in an application-wide file
Hope this is helpful for someone with similar requirements.
-- Tom
I found that the helper_method was not necessary using the american_date gem
http://stackoverflow.com/questions/14012584/convert-standard-rails-ruby-time-to-american-format-dates-getting-mixed-up
I ran into a similar issue and solved it using the
delocalize
gem: http://stackoverflow.com/questions/15531786/how-do-i-change-the-format-activerecord-expects-when-parsing-dates-from-a-text-fPlease help!! I have followed the tutorial verbatim but the published_on date is not showing up in calendar view. Unless i put this code in articles_controller.rb and it only shows entries for current days only. @articles_by_date = Article.all.group_by {|i| i.created_at.to_date}.
Is there a way to show published_on date for current/past and future dates also?
Any ideas on how to use Ryans scheduler with ajax calls to update month instead of the page reload links.
+1
Ok, I've done it:
_calendar.html.erb:
<%= link_to "<", {date: @date.prev_month}, id: "calendar-main-month-left-arrow", remote: true %>
<%= @date.strftime("%B %Y") %>
<%= link_to ">", {date: @date.next_month}, id: "calendar-main-month-right-arrow", remote: true %>
<%= render "calendar_days" %>
_calendar_days.html.erb:
(Here you place just the rendering of the helper starting with
<%= calendar @date do |date| %>
and finishing with
<% end %>
date.js.coffee:
$("#calendar-main-month-left-arrow").attr("href", "/?date=<%= @prev_date %>&prev=true")
$("#calendar-main-month-right-arrow").attr("href", "/?date=<%= @next_date %>&next=true")
$("#calendar-days").replaceWith("<%= j render 'calendar_days' %>")
$("#calendar-main-month-span").html("<%= j @date.strftime('%B %Y') %>")
and in your controller:
if params[:date]
@prev_date = Date.strptime(params[:date],"%Y-%m-%d") - 1.month
@date = Date.strptime(params[:date],"%Y-%m-%d")
@next_date = Date.strptime(params[:date],"%Y-%m-%d") + 1.month
render "date"
end
Let me know if it helps ;), you can help yourself with episode #136 (revised)
so I have a datetime field in my modle and I have seperated them by:
and this works fine. I have then change the date select to a
f.text_field
and applied whats discussed in the video. and I produce this error:1 error(s) on assignment of multiparameter attributes
If I comment out the
f.time_select
line like so:everything works fine except that the time is saved as
00:00:00
of course. Any one have any clue on how to get this working right?I have since separated the two since it was causing even more problems when I was plotting the events on the calendar. I know its not the best solution to separate the date and time as two data fields but I got it working rather painless.
Still would love to hear any better ideas
I'm using haml and had to change the call to
calendar
to be of the display format (I had to do= calendar do |date|
instead of- calendar do |date|
). Is this expected? Ryan had the call tocalendar
marked as nondisplaying code, but had it the other way initially.I also ran into this. Initially I saw nothing on screen. Changed:
<% calendar do |date| %>
to
<%= calendar do |date| %>
All is well!
Thanks, i was looking how to fix that
I was getting a wrong argument error (1 for 0) on
<%= calendar ....%>
and upgrading from rails 3.1.8 to rails 3.2 solved that issue.Upgrading to 3.2 was screwing other things up for me (I'm on Windows 7), so I just commented out the (START_DAY) arguments in the weeks method.
As long as I don't need to manually set the start of the week, that should be fine for now.
Tip: I had to restart my rails server after I added the "require" lines to the CSS and javascript files. Rails was throwing an error that it could not find those files until I did this.
It is articles_controller.rb not calendars_controller.rb
I want to change the heading of calendar as: Sun, Mon and so on. what is the idea?
This is great, i love that it isn't a gem so I'm more likely to read the code & learn.
I'd like to add to this to allow for events that occur over multiple days. Does anybody have any advice on how to go about this?
I'm combining this with dynamic forms from episode #403. The jQuery doesn't fire for the properties when looping through the following lines...
The datepicker works perfectly for anything being edited that isn't within properties. Troubleshooting it seems that the issue is jQuery being called/attached/whatever-term-you-want before the builder finishes. If I add the following lines, the name will trigger its changes, but the date will not.
How does one properly include the jQuery for render calls?
Hi all,
my jquery datepicker didn't show without page refresh. I followed the exact step and I don't understand why. Any Idea please.
This is awesome. The helper class is just fantastic - very flexible and easily extendible.
Thanks Ryan.
I have successfully implemented the calendar with a grouping by start date. However, my events span over weeks. How can I convert my events to span from start date to end date?
Thanks!
Were you able to figure this out?
I figured out the issue with coffeescript not woking properly. On a Rails 4 app you will run into problems where it appears there is some conflict with turbolinks. Just use jquery.turbolinks and you should be good. Find the solution here: http://stackoverflow.com/questions/17881384/jquery-gets-loaded-only-on-page-refresh-in-rails-4-application
The calendar is working great but when i deploy it (using Heroku), the current date is wrong. It's set to a day ahead. What's wrong??
You'll need to set the time zone to the current user's time zone. It's probably setting it to Heroku's UTC time.
Before creating the calendar, set the time zone in the controller.
Hey guys - i've been trying to do this for a couple of days now and haven't been getting anywhere. I have an advertisers view - i can see all advertisers i've created. I want to be able to select a date/date range at the top of the advertisers list and show advertisers for a particular day/date range. I'm sure there is a way to modify this to be able to do that. I've been using the created_at method and have even looked at by_star gem. not sure how to generate the different views though - I'm new in rails so still unsure of how everything works. Any help is appreciated. Thanks in advanced.
First. I'm new to rails. Besides that, I could make it just fine. I even figured out how to show just a week, instead of the hole month.
But I need to create another table inside each day, with the hours of the day On the left, and the tasks for the respective hour on the right. Can anyone help me, pretty please?
hey, if you have a second, could you help me out on how you got it just to show a week?
hi everyone , this is the first time I ever post something here but before I proceed I need to thank every one kind soul that takes the time to help others, like me, not so experienced; thank you thank you thank you...now I have an app that before I add the code above(calendar) was working fine but now it only works on localhost...I am assuming this something related to heroku so
I looked at heroku logs looking for some hints but I can't figure it out...here is the logs9 at least the first 5 lines)
Starting process with command
bin/rails server -p 4543 -e $RAILS_ENV
2013-12-02T22:10:36.246379+00:00 app[web.1]: /app/config/application.rb:5:in
<top (required)>': undefined local variable or method
config' for main:Object (NameError)This helper should go down in the Rails Gallery of Art/Hall of Fame- It looks so simple, but there so much going on right in front of your face.... I have revisited it several time to remember all of the mechanisms. These patterns have helped me trim a lot ofugly out from my views. Thanks!!!
If your calendar isn't showing up, make sure you use
<%= calendar
instead of<% calendar
<div id="articles">
<%= calendar do |date| %>
<%= date.day %>
<% end %>
</div>
Thank you! Was scratching my head there, but now all is well. Sometimes it's the simplest things …
hi everyone I am rails beginner I need some modifications in the calendar how do change this calendar with link that when I click an any date it has to show the corresponding day below the calendar.....please share your ideas...
Hey everybody. It's 2014, and as this episode is > 2 years old, a couple things have changed with the jquery-ui-rails gem:
//= require jquery-ui/datepicker
Source: https://github.com/joliss/jquery-ui-rails
I hope that helps someone... :)
I did the calendar_helper just like ryan but when i execute it, all it outputs is the text 'false' as well as an empty table... any thoughts where my problem might be?
Estou com este mesmo problema... :(
Does any body know a way of dragging different articles to different days - to change the dates of those particular records?