0

我想將FullCalendar加入到我的Rails應用中。但拖拽後我保存時遇到問題。完整日曆:ActiveRecord :: RecordNotFound(找不到與'id'=更新的事件)

我有2個模型 - 事件和鍛鍊。在事件下,有鍛鍊。

我能夠完整啓動並運行日曆並保存事件,但是當我拖放並刷新時,事件將恢復爲原始時間。

在我的控制檯,它說:

無法與 'ID'=更新找到事件。

當事件通過表單創建並保存時,它們會在日曆上正確顯示並保存。只是當我嘗試拖放。

這是我的事件控制器。

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?

謝謝!

回答

0

Rails應用程序是假設update:id當你打這個URL /events/update

$ rake routes 

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

改變Ajax請求的URL

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

啊謝謝!現在我的終端說 ActionController :: ParameterMissing(參數丟失或值爲空:事件): app/controllers/events_controller.rb:79:in'event_params' app/controllers/events_controller.rb:49:in 'block in update' app/controllers/events_controller.rb:48:'update' 這是否意味着我需要更新我的更新方法? –

+0

它仍然在說同樣的消息。難道是因爲我沒有start_time和end_time而沒有日期嗎?雖然我認爲start_time和end_time包含日期.. –

+0

您必須將參數更改爲:start_time nad:end_time –

相關問題