I am using the FullCalendar library by Adam Shaw for my Rails project.
Currently, I have it so that when you click on a date, it prompts you to input a title for the event (and possibly other fields later down the road) using the select
callback.
To store the events, I am using a SQLite database model event
.
I would like to POST
additional fields (such as type:string
to my model that are not the default parameters for an EventObject
in FullCalendar.
For some reason this works:
// JS file associated with the calendar div
$('#calendar').fullCalendar({
...
select: function(start, end, allDay){
var title = prompt('Event title:');
if (title){
var eventData = {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: ''
};
$.ajax({
url: '/events',
type: 'POST',
data: {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: '',
type: '' // <------ I can POST this when it's empty/nil
},
success: function(resp){ // <------ this is called
alert('success!');
$('#calendar').fullCalendar('renderEvent', eventData, true);
},
error: function(resp){
alert('failure!');
}
});
}
$('#calendar').fullCalendar('unselect');
},
});
But not when type:
is a non-empty string:
$.ajax({
url: '/events',
type: 'POST',
data: {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: '',
type: 'custom' // <------ This gives me an alert with 'failure!'
},
success: function(resp){
alert('success!');
$('#calendar').fullCalendar('renderEvent', eventData, true);
},
error: function(resp){
alert('failure!'); // <--- this is called
}
});
How would I go about posting these non-default fields?
Here is the POST
and event_params
code from /events_controller.rb
(note: I am using Devise
for login system, therefore current_tutor
just retrieves the currently-logged in user/tutor; tutor has_many :events
and event belongs_to :tutor
):
class EventsController < ApplicationController
...
def create
@tutor = current_tutor
@event = @tutor.events.build(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
...
def event_params
params[:start_time] = params[:start]
params[:end_time] = params[:end]
params.permit(:title, :description, :start_time, :end_time, :url, :type)
end
end