In a previous post, I talked about setting up Google’s OAuth with a Rails app. Today I am going to go through the complete steps of adding, updating, and deleting a event from an Event Dashboard in your Rails app that will also add, update, and delete that event in a corresponding Google Calendar (I am adding, updating, and deleting events to my personal calendar, but you can change the calendarId option in the parameters to any calendar that you have admin access to).
Step 1: Getting your Event Model Ready for Google Calendar
The first thing we need to do is to add another attribute to our events model so that we can store the google event id after we create the event and push it to our Google Calendar. For simplicity, I called this google_event_id and gave it a string attribute.
Step 2: Creating an Event
We need to update the create action in our events controller so that after we make the API call to add the event to the Google Calendar, we save the google event id to the event object. We will need this google event id in the future to update and delete google calendar events from our Rails app.
Step 3: Updating an Event
Now that we have a google_event_id as an attribute of each newly created event model, we can query the Google Calendar API for that specific event and update it in our events controller. I gave a conditional in case there is no google_event_id (for historical events that were not tied to the google calendar)
Step 4: Deleting an Event
Similarly, with the google_event_id attribute of the event object, you can delete an event from the events controller.