前提・実現したいこと
rails初心者で初投稿です。よろしくお願いいたします。
railsでfullcalendarのgemを使って予定表アプリを作成中です。
予定を追加してカレンダー上で表示させたいのですが、追加した予定がカレンダーに表示されません。
DBへの保存は確認済みです。
発生している問題・エラーメッセージ
ローカルサーバー内
Processing by EventsController#create as HTML Parameters: {"authenticity_token"=>"cSpQFKtrYoiS7CIJM2OH/Ch1eEmmUojJ776LgQlnLOR3GEJ8K6vS4+q6KTM1UdtvK+ZGbQ+NcFbcNAI4woI1PA==", "event"=>{"title"=>"テスト15", "start_time"=>"2021-01-09T20:45", "end_time"=>"2021-01-09T22:44", "body"=>"テスト15", "consumption"=>"", "item"=>"0", "user_id"=>"1"}, "commit"=>"登録する"} Unpermitted parameter: :item (0.3ms) BEGIN ↳ app/controllers/events_controller.rb:38:in `create' User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 ↳ app/controllers/events_controller.rb:38:in `create' Event Create (0.3ms) INSERT INTO `events` (`title`, `start_time`, `end_time`, `body`, `user_id`, `created_at`, `updated_at`) VALUES ('テスト15', '2021-01-09 20:45:00', '2021-01-09 22:44:00', 'テスト15', 1, '2021-01-09 11:44:27.221455', '2021-01-09 11:44:27.221455') ↳ app/controllers/events_controller.rb:38:in `create' (1.2ms) COMMIT ↳ app/controllers/events_controller.rb:38:in `create' Redirected to http://localhost:3000/events/36 Completed 302 Found in 11ms (ActiveRecord: 2.3ms | Allocations: 4382) Started GET "/events/36" for ::1 at 2021-01-09 20:44:27 +0900 Processing by EventsController#show as HTML Parameters: {"id"=>"36"} User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/events_controller.rb:80:in `move_to_index' Event Load (0.3ms) SELECT `events`.* FROM `events` WHERE `events`.`id` = 36 LIMIT 1 ↳ app/controllers/events_controller.rb:76:in `set_event' Rendering events/show.html.erb within layouts/application Rendered events/show.html.erb within layouts/application (Duration: 0.7ms | Allocations: 143) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 11ms (Views: 7.4ms | ActiveRecord: 0.6ms | Allocations: 6263) Started GET "/events.json?start=2020-12-27T00%3A00%3A00%2B09%3A00&end=2021-02-07T00%3A00%3A00%2B09%3A00" for ::1 at 2021-01-09 20:44:27 +0900 Processing by EventsController#index as JSON Parameters: {"start"=>"2020-12-27T00:00:00+09:00", "end"=>"2021-02-07T00:00:00+09:00"} Rendering events/index.json.jbuilder Rendered events/index.json.jbuilder (Duration: 0.7ms | Allocations: 1000) Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms | Allocations: 1325)
該当のソースコード
events_controller
class EventsController < ApplicationController before_action :move_to_index, except: [:index, :new, :create] before_action :set_event, only: [:show, :edit, :update, :destroy] def index @event = Event.all # respond_to do |format| # format.html # index.html.erb # format.xml { render :xml => @event } # format.json { render :json => @event } # end end def show @event = Event.all respond_to do |format| format.html format.json { render json: @event.to_json( only: [:title, :start, :end] ) } end end def new @event = Event.new end def create @event = Event.new(event_params) if @event.save respond_to do |format| format.html { redirect_to @event, notice: '予定を作成しました' } format.json { render :show, status: :created, location: @event } end else format.html { render :new } format.json { render json: @event.errors, status: :unprocessable_entity } end end def update respond_to do |format| if @event.update(event_params) format.html { redirect_to @event, notice: '予定を変更しました' } 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: '予定を削除しました' } format.json { head :no_content } end end end private def event_params params.require(:event).permit(:title, :start_time, :end_time, :body, :user_id, :consumption) end def set_event @event = Event.find(params[:id]) end def move_to_index unless user_signed_in? redirect_to action: :index end end
calendar.js
// import "bootstrap/dist/css/bootstrap.css"; // import "@fontawesome/fontawesome-free/css/all.css"; import { Calendar, startOfDay } from "@fullcalendar/core"; import dayGridPlugin from "@fullcalendar/daygrid"; import interactionPlugin from "@fullcalendar/interaction"; import jaLocale from "@fullcalendar/core/locales/ja"; import timeGridPlugin from "@fullcalendar/timegrid"; import listPlugin from "@fullcalendar/list"; import bootstrapPlugin from "@fullcalendar/bootstrap"; document.addEventListener("DOMContentLoaded", function () { var calendarEl = document.getElementById("calendar"); var calendar = new Calendar(calendarEl, { plugins: [ dayGridPlugin, interactionPlugin, timeGridPlugin, listPlugin, bootstrapPlugin, ], initialView: "dayGridMonth", headerToolbar: { left: "prevYear,prev,next,nextYear today", center: "title", right: "addEventButton dayGridMonth,timeGridWeek,timeGridDay listMonth", }, customButtons: { addEventButton: { text: "予定の追加", // click: function() { // ('#add-event').click(function(){ // ('#inputScheduleForm').fadeIn(); // }); // ('.close-modal').click(function(){ // ('#inputScheduleForm').fadeOut(); // }); // } click: function () { var dateStr = prompt("予定を入力してください"); var date = new Date(dateStr + "T00:00:00"); if (!isNaN(date.valueOf())) { // valid? calendar.addEvent({ title: "dynamic event", start: date, allDay: true, }); alert("Great. Now, update your database..."); } else { alert("Invalid date."); } }, }, }, navLinks: true, selectable: true, dateClick: function (info) { alert("clicked " + info.dateStr); }, select: function (info) { alert("selected " + info.startStr + " to " + info.endStr); }, locale: jaLocale, //timezone: "Asia/Tokyo", height: "100vh", editable: true, defaultDate: "local", dayMaxEvents: true, // when too many events in a day, show the popover events: '/events.json', // events: [ // { // title: "テスト", // start: "2021-01-13" // } // ], }); calendar.render(); });
index.json.jbuilder
json.array!(@events) do |event| json.extract! event, :id, :title, :body, json.start event.start_time json.end event.end_time json.url event_url(event, format: :html) end
routes.rb
Rails.application.routes.draw do devise_for :users root to: "entrances#index" resources :events end
試したこと
jsonファイル内やJSファイルのevents:部分の記述を変えてみましたが変わりませんでした。
ご教授いただけると大変助かります。
補足情報(FW/ツールのバージョンなど)
ruby2.6.5
あなたの回答
tips
プレビュー