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

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Richard, could you take a look at this: stackoverflow.com/questions/34323316/… ? 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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.