So, after much research, I found the right way to get it working with fullcanlendar with rails, so I will give you the code. This is an example of using the weekly calendar.
If your language is English, then ignore the name changes, but if you do not, you can use it to change it to your language. monthNames, monthNamesShort, dayNames, dayNamesShort, and the Today button in buttonText.
In the "username" you can check everything you want, such as a name or other parameter.
calendar.js
var updateEvent; $(document).ready(function() { var todayDate = new Date(); todayDate.setHours(0,0,0,0); $('#calendar').fullCalendar({ editable: false, slotEventOverlap: false, monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'], monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'], dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'], dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb'], columnFormat: { month: 'dddd', week: 'dddd d', day: 'ddd' }, buttonText: { today: 'Hoy', month: 'month', week: 'week', day: 'day' }, minTime: "08:00:00", maxTime: "23:00:00", header: { left: 'prev,next today', center: 'title', right: '' }, firstDay: 1, //this section is triggered when the event cell it clicked selectable: true, selectHelper: true, select: function(start, end) { var user_name; user_name = prompt("User name: "); var eventData; //this validates that the user must insert a name in the input if (user_name) { eventData = { title: "Reserved", start: start, end: end, user_name: user_name }; //here i validate that the user can't create an event before today if (eventData.start < todayDate){ alert('You can't choose a date that already past.'); $("#calendar").fullCalendar("unselect"); return } //if everything it ok, then the event is saved in database with ajax $.ajax({ url: "events", type: "POST", data: eventData, dataType: 'json', success: function(json) { alert(json.msg); $("#calendar").fullCalendar("renderEvent", eventData, true); $("#calendar").fullCalendar("refetchEvents"); } }); } $("#calendar").fullCalendar("unselect"); }, defaultView: 'agendaWeek', allDaySlot: false, height: 500, slotMinutes: 30, eventSources: [ { url: '/events' } ], timeFormat: 'h:mm t{ - h:mm t} ', dragOpacity: "0.5" }); };
There are 4 attributes in the event model that are strictly necessary to make it work; these are the variables start, end, title and allDay.
The as_json method retrieves each event from the database and sends it to each cell in the calendar, so the url attribute will be a link to the "display" of each event, so if you click on this link, it will send you this opinion (which is not in this example).
event.rb (model)
class Event < ActiveRecord::Base # scope :between, lambda {|start_time, end_time| {:conditions => ["? < starts_at and starts_at < ?", Event.format_date(start_time), Event.format_date(end_time)] }} def self.between(start_time, end_time) where('start_at > :lo and start_at < :up', :lo => Event.format_date(start_time), :up => Event.format_date(end_time) ) end def self.format_date(date_time) Time.at(date_time.to_i).to_formatted_s(:db) end def as_json(options = {}) { :id => self.id, :title => self.title, :start => start_at.rfc822, :end => end_at.rfc822, :allDay => allDay, :user_name => self.user_name, :url => Rails.application.routes.url_helpers.events_path(id), :color => "green" } end end
There is nothing to add, the code does not require explanation.
events_controller.rb
class EventsController < ApplicationController def new @event = Event.new respond_to do |format| format.html format.js end end def create @event = Event.new(event_params) if @event.save render json: {msg: 'your event was saved.'} else render json: {msg: 'error: something go wrong.' }, status: 500 end end def index @events = Event.between(params['start'], params['end']) if (params['start'] && params['end']) respond_to do |format| format.html format.json { render :json => @events } end end def event_params params.permit(:title).merge start_at: params[:start].to_time, end_at: params[:end].to_time, user_name: params[:user_name] end
create event migration
class CreateEvents < ActiveRecord::Migration def change create_table :events do |t| t.string :title t.datetime :start_at t.datetime :end_at t.string :allDay t.string :user_name t.timestamps end end end
In this example, you can save and show your events without problems in the rails, but if you want to change the way data is displayed in the event, I would recommend that you change the css properties in the calendar, for example
if you want to show only the title in the event without beginning and end, you should do this:
.fc-event-time { display: none; }
And if you want to center the name:
.fc-event-title{ text-align: center; }
As you can see, this is quite simple, and you do not need to change the source code of the full calendar.
Hope this helps !: D !!