0

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
Richard
  • 828
  • 1
  • 8
  • 28

1 Answers1

0

As a testament to my inexperience and naivety, I came to the realisation that your data shouldn't contain a reserved key used by the ajax request.

Changed the type field to post_type and it worked like a charm. DOH!

Richard
  • 828
  • 1
  • 8
  • 28
  • Richard, could you take a look at this: http://stackoverflow.com/questions/34323316/fullcalendar-passing-js-vars-to-rails-and-vice-versa ? I have an extra question here as well. When you do the AJAX POST request with this args start: start.format(), but in your schema it is :start_at? Why do you have to do this way + with params[:start_time] = params[:start] in your events controller private section? Can not you just directly use start_time: start in the AJAX req? – Sean Magyar Dec 17 '15 at 21:15
  • I had problems before posting without reassigning params in the controller, but it might've trickled down from the issue above. Looking back, I think what I did was wholly unnecessary. – Richard Dec 18 '15 at 22:23