Simple_calendar and Rails

May 2024 ยท 2 minute read

Simple Calendar is designed to do one thing really well: render a calendar

I assume that you have installed a rails app and want to add a calendar.

First add simple calendar to your Gemfile

gem "simple_calendar", "~> 2.0"

Include the default stylesheet for the calendar in your app/assets/stylesheets/application.css file:

*= require simple_calendar

Generate a calendar for the month with the month_calendar method:

 <%= month_calendar do |date| %> <%= date %> <% end %> 

To add events to the calendar create a model named meeting:

$ rails g model Meeting
 class CreateMeetings < ActiveRecord::Migration[5.1] def change create_table :events do |t| t.date :start_time t.string :name t.timestamps end end end 

And controller:

$ rails g controller meetings

Then do the migration:

$ rails db:migrate

Add meetings to the routes:

resources :meetings

Add the form to create a meeting:

 <%= form_for :meeting, url: metings_path do |f| %> <p> <%= f.label :name %> <%= f.text_field :name %> </p> <p> <%= f.label :date_and_hour %> <%= f.datetime_field :start_time %> </p> <p> <%= f.submit "Safe Meeting" %> </p> <% end %> 

Add the method index and create to the conroller

 def index @meetings = Meeting.all end def create Meeting.create(meeting_params) redirect_to calendars_path end private def meeting_params params.require(:meeting).permit(:name, :start_time) end 

And change the month_calendar method:

 <%= month_calendar events: @meetings do |date, meetings| %> <%= date %> <% meetings.each do |meeting| %> <div> <%= meeting.name %> </div> <% end %> <% end %> 

and thats it !!

ncG1vJloZqxrXpa6osbOp5iwq16YvK57zJqrsqelYsCwuNStoKimo2TAsLjUraCopqNksbCv1Kacp6yjZH1xfI5pZ2lnYGeEcLvRop6ippGhfLS1zKmjnpeTlrmmusOaqWegpKK5gH2Ua3BucGlthHo%3D