简体   繁体   中英

RoutingError on ajax call to update fullcalendar event

i'm using fullcalendar to show events on my rails application. Works fine. Now i want the events to update in the database on drag and drop actions. For that i'm using eventDrop method and an ajax call based on this answer:

eventDrop: function(event, delta, revertFunc) {
            update_source = update_prefix + event.user_id + "/appointments/" + event.id;
            $.ajax({
                type: "POST",
                dataType: "script",
                url: update_source,
                contentType: 'application/json',
                data: JSON.stringify({ resource:{start:event.start, end: event.end}, _method:'put' })
            }).done(function( msg )
            {
                alert( "Data Saved: " + msg );
            });
        }

This is update method on the controller:

def update
  @appointment.update(appointment_params)
  if @appointment.save
    respond_to do |format|
      format.js
    end
  else
    render js: "alert('Please include phone number');"
  end
end

And these are the routes on rake routes:

PATCH  (/:locale)/companies/:company_id/users/:user_id/appointments/:id(.:format)      appointments#update
PUT    (/:locale)/companies/:company_id/users/:user_id/appointments/:id(.:format)      appointments#update
DELETE (/:locale)/companies/:company_id/users/:user_id/appointments/:id(.:format)      appointments#destroy

However i keep getting the following Error:

ActionController::RoutingError (No route matches [POST] "/es/companies/1/users/50/appointments/140"):

Just in case; there is actually an appointment with id 140 belonging to a user 50 belonging to company 1 in the database.

Thanks

Changing your ajax request to PUT or PATCH will solve your issue, however I am not sure this is the intention. I mean:

        $.ajax({
            type: "PATCH",
            dataType: "script",
            url: update_source,
            contentType: 'application/json',
            data: { resource:{start:event.start, end: event.end} }
        }).done(function( msg )
        {
            alert( "Data Saved: " + msg );
        });

It will call update action. Also, you do not need JSON.stringify , just pass data {...} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM