0

I'm following a tutorial, here where I am trying to get a modal to come up to create a new event when a date is clicked. I can't get the modal to appear.

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>JandelSchedule</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <header>
      <%= render 'layouts/navigation'%>
    </header>
    <main role="main">
      <div class="container">
        <%= yield %>
      </div>
      <div id="remote-container">
    </main>
  </body>
</html>

I am using an empty div with id remote container to place the _new.html.erb modal partial into when the user clicks on a date in the calendar.

calendar.js
import { Calendar } from '@fullcalendar/core';
import interactionPlugin from '@fullcalendar/interaction';
import dayGridPlugin from '@fullcalendar/daygrid';
import bootstrapPlugin from '@fullcalendar/bootstrap';

document.addEventListener('turbolinks:load', function() {
  var calendarEl = document.getElementById('calendar');

  var calendar = new Calendar(calendarEl, {
    plugins: [ interactionPlugin, dayGridPlugin, bootstrapPlugin ],
    themeSystem: 'bootstrap',
    initialView: 'dayGridMonth',
    headerToolbar: {
      left: 'prev,next today',
      center: 'title',
      right: 'dayGridMonth,dayGridWeek,dayGridDay'
    },
    selectable: true,
    selectHelper: true,
    editable: true,
    eventLimit: true,
    events: '/events.json',

    select: function(start, end) {
      $.getScript('/events/new', function() {

      });
    },
  });
  
  calendar.render();
});

The event should run this file. I think the error is here but can't figure out why.

new.js.erb
$('#remote-container').html('<%= j render "new" %>');
$('#new-event').modal('show');

Here is the modal.

_new.html.erb
<div class="modal" id="new-event">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Create New Event</h4>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      </div>
      <div class="modal-body">
        <%= render 'form', event: @event %>
      </div>
    </div>
  </div>
</div>

Lastly, here is the controller

events_controller.rb
class EventsController < ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy]
  
  def index
    @events = Event.where(start: params[:start]..params[:end])
  end

  def show
  end

  def new
    @event = Event.new
  end

  def edit
  end

  def create
    @event = Event.new(event_params)
    @event.save
  end

  def update
    @event.update(event_params)
  end

  def destroy
    @event.destroy
  end
  
  private
    def set_event
      @event = Event.find(params[:id])
    end

    def event_params
      params.require(:event).permit(:title, :date_range, :start, :end, :color)
    end
end

Console output when date is clicked

Started GET "/events/new?_=1607411496171" for ::1 at 2020-12-08 00:11:37 -0700
Processing by EventsController#new as JS
  Parameters: {"_"=>"1607411496171"}
  Rendering events/new.js.erb
  Rendered events/_form.html.erb (Duration: 20.3ms | Allocations: 6111)
  Rendered events/_new.html.erb (Duration: 23.2ms | Allocations: 6463)
  Rendered events/new.js.erb (Duration: 25.7ms | Allocations: 6946)
Completed 200 OK in 33ms (Views: 30.5ms | ActiveRecord: 0.0ms | Allocations: 8496)

EDIT: here is the browser console output

main.js:9223 Unknown option 'selectHelper'
warnUnknownOptions @ main.js:9223
main.js:9223 Unknown option 'eventLimit'
warnUnknownOptions @ main.js:9223
VM12:1 Uncaught ReferenceError: $ is not defined
    at <anonymous>:1:1
    at DOMEval (jquery.js:109)
    at Function.globalEval (jquery.js:313)
    at textScript (jquery.js:8849)
    at ajaxConvert (jquery.js:8100)
    at done (jquery.js:8503)
    at XMLHttpRequest.<anonymous> (jquery.js:8774)
calendar.js:25 $('#remote-container').html('<div class=\"modal\" id=\"new-event\">\n  <div class=\"modal-dialog\">\n    <div class=\"modal-content\">\n      <div class=\"modal-header\">\n        <h4 class=\"modal-title\">Create New Event<\/h4>\n        <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;<\/button>\n      <\/div>\n      <div class=\"modal-body\">\n        <form class=\"simple_form new_event\" id=\"new_event\" novalidate=\"novalidate\" action=\"/events\" accept-charset=\"UTF-8\" data-remote=\"true\" method=\"post\">\n  <div class=\"form-inputs\">\n    <div class=\"form-group string optional event_title\"><label class=\"string optional\" for=\"event_title\">Title<\/label><input class=\"form-control string optional\" type=\"text\" name=\"event[title]\" id=\"event_title\" /><\/div>\n    <div class=\"form-group select optional event_color\"><label class=\"select optional\" for=\"event_color\">Color<\/label><select class=\"form-control select optional\" name=\"event[color]\" id=\"event_color\"><option value=\"\"><\/option>\n<option value=\"black\">Black<\/option>\n<option value=\"green\">Green<\/option>\n<option value=\"red\">Red<\/option><\/select><\/div>\n  <\/div>\n\n  <div class=\"form-actions\">\n    <input type=\"submit\" name=\"commit\" value=\"Create Event\" class=\"btn\" data-disable-with=\"Create Event\" />\n    \n  <\/div>\n<\/form>\n      <\/div>\n    <\/div>\n  <\/div>\n<\/div>');
$('#new-event').modal('show');

Edit: modal error

Uncaught TypeError: $(...).modal is not a function
at <anonymous>:2:17
at DOMEval (DOMEval.js:38)
at Function.globalEval (core.js:197)
at textScript (script.js:19)
at ajaxConvert (ajax.js:249)
at done (ajax.js:652)
at XMLHttpRequest.<anonymous> (xhr.js:75)

Any help would be greatly appreciated!

Thanks

jive14
  • 7
  • 6
  • Maybe you can try `$('#remote-container').html('<%= escape_javascript(render 'new') %>');` – Victor Luna Dec 08 '20 at 14:09
  • Just tried that and it is still the same. – jive14 Dec 09 '20 at 04:51
  • Any other ideas? – jive14 Dec 10 '20 at 05:27
  • I can try helping you debug but I am not a 100% sure. What do you get if you do the folllowing `$.getScript( '/events/new', function( data ) { console.log( data ) ;});` – Victor Luna Dec 10 '20 at 13:06
  • Okay, I added that but nothing shows any different in the console - it is identical to what I put in the questions. Just to make sure, the code I pasted in above would show in my development log right? – jive14 Dec 12 '20 at 05:37
  • No, this appears in the javascript console on the browser. I think this is a Javascript issue, since when you click on it, rails renders it but the form does not show. – Victor Luna Dec 13 '20 at 15:47
  • Thanks, I didn't know about the browser console. I made an edit to the question to show the errors. My first guess is I am using options from a different version of FullCalendar. Thoughts? – jive14 Dec 14 '20 at 04:26
  • Okay, I got past Uncaught ReferenceError: $ is not defined error. I had to add $: 'jquery/src/jquery', in my webpack/environment.js file. Next error that pops up is Uncaught TypeError: $(...).modal is not a function and I have no idea what to do here – jive14 Dec 14 '20 at 05:17
  • modal function comes from bootstrap, do you have bootstrap in your assets? – Victor Luna Dec 14 '20 at 13:50
  • I have added *=bootstrap to my application.css.scss file and I have added import 'bootstrap' to my application.js file. I know bootstrap is working using because I am using the navbar, including the dropdown menus. Added the error to my question. – jive14 Dec 15 '20 at 04:59
  • Javascript webpacker issue. Answer is here: https://stackoverflow.com/questions/60470917/tooltip-is-not-a-function-rails-6-webpack – jive14 Dec 16 '20 at 05:42

0 Answers0