完整日历:ActiveRecord :: RecordNotFound(无法找到'id'= update的事件)

时间:2016-07-31 22:40:26

标签: javascript ruby-on-rails ajax javascript-events fullcalendar

我正在尝试将FullCalendar包含在我的rails应用中。但是拖放后我很难保存。

我有2个模特 - 活动和锻炼。在活动中,有锻炼。

我可以启动并运行完整日历并保存事件,但是当我拖放,然后刷新时,事件将恢复到原始时间。

在我的控制台中,它说:

  

找不到'id'= update。

的事件

通过表单创建和保存事件时,它们会正确显示并保存在日历上。就在我尝试拖放的时候。

这是我的事件控制器。

class EventsController < ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  def index
    @events = current_user.events
  end

  def show
    @event = Event.find(params[:id])
  end

  def new
    @event = current_user.events.new
  end

  def edit
    @event = current_user.events.find(params[:id])
  end

  def create
    @event = current_user.events.new(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 update
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event, notice: 'Event was successfully updated.' }
        format.json { render :show, status: :ok, location: @event }
      else
        format.html { render :edit }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @event.destroy
    respond_to do |format|
      format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_event
    @event = Event.find(params[:id])
  end

  def event_params
    params.require(:event).permit(:title, :description, :start_time, :end_time, :workouts_attributes => [:id, :name, :category, :_destroy])
  end
end

下面是控制日历的application.js文件。

$(document).on('turbolinks:load', function() {
    $('#calendar').fullCalendar({
      editable: true,
      events: '/events.json',
      eventDrop: function(event, delta) {
        $.ajax({
          type: "PUT",
          url: '/events/update',
          dataType: 'json',
          data: {id:event.id, start:event.start_time, end: event.end_time}
        });
      },
    });
});

当我尝试拖放时,我会在下面收到一条消息:

ActiveRecord::RecordNotFound (Couldn't find Event with 'id'=update):
  app/controllers/events_controller.rb:74:in `set_event'

当我检查我的控制台时,所有事件都有ID。但我猜也许当事件被拖放时,新事件没有ID?

谢谢!

1 个答案:

答案 0 :(得分:0)

当您点击此网址时,

Rails应用假定update:id /events/update

$ rake routes

event  PUT  /events/:id(.:format)          events#update

将ajax请求中的网址更改为

$.ajax({
  type: 'PUT',
  url: '/events/' + event.id,
  dataType: 'json',
  data: { event: { start_time:event.start_time, end_time: event.end_time } }
});