How to post additional parameters via JQuery with FullCalendar in Rails? -
i using fullcalendar library adam shaw rails project.
currently, have when click on date, prompts input title event (and possibly other fields later down road) using select
callback.
to store events, using sqlite database model event
.
i post
additional fields (such type:string
model not default parameters eventobject
in fullcalendar.
for reason works:
// js file associated 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: '' // <------ can post when it's empty/nil }, success: function(resp){ // <------ called alert('success!'); $('#calendar').fullcalendar('renderevent', eventdata, true); }, error: function(resp){ alert('failure!'); } }); } $('#calendar').fullcalendar('unselect'); }, });
but not when type:
non-empty string:
$.ajax({ url: '/events', type: 'post', data: { title: title, description: '', start: start.format(), end: end.format(), url: '', type: 'custom' // <------ gives me alert 'failure!' }, success: function(resp){ alert('success!'); $('#calendar').fullcalendar('renderevent', eventdata, true); }, error: function(resp){ alert('failure!'); // <--- called } });
how go posting these non-default fields?
here post
, event_params
code /events_controller.rb
(note: using devise
login system, therefore current_tutor
retrieves currently-logged in user/tutor; tutor has_many :events
, event belongs_to :tutor
):
class eventscontroller < applicationcontroller ... def create @tutor = current_tutor @event = @tutor.events.build(event_params) respond_to |format| if @event.save format.html { redirect_to @event, notice: 'event 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
as testament inexperience , naivety, came realisation data shouldn't contain reserved key used ajax request.
changed type
field post_type
, worked charm. doh!
Comments
Post a Comment