fullcalendar-rails installation not working - ruby-on-rails

I'm fairly new to Rails and programming in general, and I'm trying to add fullcalendar-rails to a fairly basic new application. Unfortunately, the install process seems to fail entirely.
Following the instructions provided at https://github.com/bokmann/fullcalendar-rails, after ensuring that javascript in general is enabled in the app (making sure I have jquery etc installed), results in an error claiming $(...).fullCalendar is not a function. This makes it seem to me that the fullcalendar source files are simply not being seen by my Rails app, and I'm not sure how to go about getting them to be seen. I tried manually adding the files to my app structure (in /lib and /vendor), and that had no effect.
For reference, my application.js is this:
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
//= require jquery
//= require moment
//= require fullcalendar
import Rails from "#rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "#rails/activestorage"
import "channels"
import "jquery"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
$('#calendar').fullCalendar({});
UPDATE: following razvans's answer, my application.js looks like this:
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "#rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "#rails/activestorage"
import "channels"
import $ from "jquery";
require "moment"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
import { Calendar } from '#fullcalendar/core';
import dayGridPlugin from '#fullcalendar/daygrid';
const rootEl = document.getElementById('calendar');
const calendar = new Calendar(rootEl, {
plugins: [dayGridPlugin]
});
calendar.render();
I'm getting the following error:
Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\prgrm\benkyou\tripplan\tripplan\app\javascript\packs\application.js: Missing semicolon. (12:7)
10 |
11 | import $ from "jquery";
> 12 | require "moment"
| ^
13 |
14 | Rails.start()
15 | Turbolinks.start()
at Parser._raise

That gem is for Asset Pipeline which you don't use. You are on webpacker and inside javascript files you need to use import/require. //= require something does not work. Feel free to remove those gems.
With webpacker you need to add packages with yarn:
yarn add #fullcalendar/core #fullcalendar/daygrid
yarn add moment
Then import it.
import $ from 'jquery';
require ("moment")
import { Calendar } from '#fullcalendar/core';
import dayGridPlugin from '#fullcalendar/daygrid';
function loadCalendar() {
var calendarEl = document.getElementById('calendar');
var calendar = new Calendar(calendarEl, {
plugins: [dayGridPlugin]
});
calendar.render();
};
$(document).on('turbolinks:load', loadCalendar);
I don't use the library but I tried it just for this. Here's how it looks :)

Related

Rails 6 select2 is not defined

Im using rails 6 with webpack, and for some reason I cannot use select2 when I try to respond a js file the following:
$("#select-search-bar").select2()
How can I manage to use select2 in the js respond
Here is my application.js file:
require("#rails/ujs").start()
require("turbolinks").start()
require("#rails/activestorage").start()
require("channels")
require('font-awesome/css/font-awesome.css')
require("jquery")
require('cocoon-rails/src/cocoon')
require('chart.js/dist/Chart')
require('materialize-css/dist/js/materialize')
require('../stylesheets/application')
require('packs/dashboards')
require('packs/relateds')
require('packs/banner')
require('packs/users')
import 'packs/select2'
require('packs/resources')
// packs/select2.js
import $ from 'jquery';
import 'select2/dist/js/select2.min'
My guess is your app/webpacker/packs/application.js is not configured correctly.
For example this is what I have:
require( 'select2' );
import "select2/dist/css/select2.min.css";
Make sure you've added Select2 with yarn add select2 see more here. It is possible to have Select2 translated as well by adding your locales like this:
require( 'select2/dist/js/i18n/en.js' );
require( 'select2/dist/js/i18n/lv.js' );
In case you need bootstrap theme, you can add it as well, for example, Bootstrap3 with yarn add select2-bootstrap-theme and then in your app/webpacker/packs/application.js
import "select2-bootstrap-theme/dist/select2-bootstrap.min.css";
In case this doesn't help, probably you have some error in your console and you might be interested to check that.

TinyMCE w/Rails 6 and webpacker - no icons, turbolinks

We are trying to upgrade our app to Rails 6 with Webpacker (and Stimulus). Things are going fine except for TinyMCE. We have 2 problems, I will ask them in separate SO questions.
We installed TinyMCE using
yarn add tinymce
and have version 5.3.0
In our stimulus controller header we have:
import tinymce from 'tinymce/tinymce';
import 'tinymce/themes/silver';
import 'tinymce/skins/ui/oxide/skin.min';
import 'tinymce/skins/ui/oxide/content.min';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/link';
And then in the controller connect block we have:
connect() {
console.log('gonna reload');
require.context(
'!file-loader?name=[path][name].[ext]&context=node_modules/tinymce&outputPath=js!tinymce/skins',
true,
/.*/
);
tinymce.init({
selector: '.tinymce',
plugins: ['paste', 'link'],
skin: false
});
}
This is code that essentially works, except that in the console I see:
VM40 application-68201fac0dcbbcb543e0.js:213771 GET https://xxx.ngrok.io/packs/js/icons/default/icons.js net::ERR_ABORTED 404 (Not Found)
VM40 application-68201fac0dcbbcb543e0.js:224775 Failed to load icons: default from url https://xxx.ngrok.io/packs/js/icons/default/icons.js
Do we need another require.context to handle the loading of those?
It looks like TinyMCE no longer loads the icons dynamically, so Webpack must be instructed to include them in the bundle manually. Adding the icons import after the tinymce import worked for me:
import 'tinymce/icons/default';

How to migrate requiring of select2.js from sprockets to webpacker

I'm in the process of migrating a Rails 5.1.5 project, which uses CoffeeScript, from using sprockets to using webpacker. The project also uses select2.js. With sprockets, I did the following:
Install jquery-rails (jQuery is a dependency for select2).
Put select2.js code in vendor/assets/javscripts.
In application.js.coffee, add:
#= require select2
After that I was able to use select2 to in my application.js.coffee file:
$(document).on 'turbolinks:load' ->
$('select').select2
So far I've described the pretty standard way of including/using javascript libraries with sprockets.
However, with webpacker I can't make select2 work and I'm not sure why. I have two hypothesis:
I'm not importing/requiring it properly;
it doesn't find jQuery at some point of the load process;
So for jQuery, I did the following:
yarn add jquery
included in my environment.js:
const webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}));
I've removed the jquery-rails gem, as well as #= require 'jquery' and tested that jquery works, so I guess I have correctly included it. However, I tried several ways of importing select2 (using es6 imports) and none of them worked. I tried:
import select2 from 'select2';
import select2 from 'select2/dist/js/select2'
import select2 from 'select2/dist/js/select2.js'
import 'select2/dist/js/select2.js'
I even tried to import it from the old vendor location by writing inside app/javascript/pack/application.js.coffee:
import '../../../vendor/assets/javascripts/select2'
I can confirm that the file contents is imported, as I put a console.log within the select2 file under node_modules/select2/dist/js/select.js and it did get printed. However, I also get the error TypeError: $(...).select2 is not a function when I execute $('select').select2() in the browser's dev tool console.
What am I missing or doing wrong?
P.S. I can provide much more info, but I didn't want my question to get any more bloated.
P.P.S. With my modest JS knowledge, I looked at the source code but couldn't recognize what exactly they are exporting and how am I supposed to import it.
I know this is an old post, but just in case someone else could benefit:
app/javascript/packs/application.js
...other requires...
require('select2')
window.Rails = Rails
import 'bootstrap'
...other imports...
import 'select2'
import 'select2/dist/css/select2.css'
$(document).on("turbolinks:load", () => {
$('.select2').select2()
})
My similar problem
I have stumble upon the same problem with another web component (Switchery):
I imported the component with yarn add switchery (no error)
I could import it correctly through WebPack with import 'switchery' (no error bundling the pack)
But when I was trying to use the Switchery object in the browser like they say in the doc:
var elem = document.querySelector('.js-switch');
var init = new Switchery(elem);
I would get the error: ReferenceError: Switchery is not defined
Note: I didn't want to install RequireJS as WebPack is supposed to do the same thing (and even better) nowadays.
My solution:
The problem was the webpack doesn't expose the pack-generated variables and classes in the global scope!
So to fix this, I needed to do two things:
Explicitly give a name to the imported class from Switchery:
import Switchery from 'switchery'
Use this Class only in the same JS file where the import was done
Testing hack:
If you want to try that out and "go back" to the mess that sprocket allowed, in the same file, you can expose "globally" the variable so you can use in from the browser:
import Switchery from 'switchery'
window.Switchery = Swicthery
now you can execute the switchery almost like in the example:
var init = new window.Switchery(elem);
Hope that helps...

jsx doesn't seem to transpile correctly

I am using the react-rails gem in a Rails(4.2.7) project. This is the only modification I've made to my default Gemfile:
gem 'react-rails'
I've defined a component in a .jsx file using es6 syntax:
class Foo extends React.Component {
render () {
return (<h1>hello</h1>)
}
}
This works fine, but when I attempt to import a js module, I get an error:
import ReactTransitionGroup from 'react-addons-transition-group'
class Foo extends React.Component {
render () {
return (<h1>hello</h1>)
}
}
foo.es6.self-69f3a42….js?body=1:11 Uncaught ReferenceError: require is not defined
It seems to me that if I'm not transpiling the jsx correctly then I would be getting an error in the first case, but I'm not. I've been using jsx all day long with no issues until I tried to use import.
I've tried adding the following to my Gemfile:
gem 'sprockets-es6'
gem 'babel-transpiler'
I've tried adding this to config/environments/development.rb:
config.react.jsx_transformer_class = React::JSX::BabelTransformer
And I've tried generating the component using the rails react component generator with the --es6 flag:
rails g react:component Foo --es6
What am I missing?
So here's how I resolved my issue: since I'm using the 'react-rails' gem, the addons are available via a configuration option - I added the following to config/application.rb:
config.react.addons = true
Then in my .jsx file, instead of import, this is the syntax I used to have access to ReactTransitionGroup:
var ReactTransitionGroup = React.addons.ReactTransitionGroup
I still don't understand, but maybe someday I will, why the import statement transpiles to require which is undefined, and how that could be fixed by anything other than defining require, or transpiling to something that is defined.

jquery-ui gem: File to import not found or unreadable: jquery-ui/autocomplete

In development, I can't figure out how to make the jquery-ui gem to make it's sweet magic available.
I included the gem in the project Gemfile, typed bundle install, updated app/assets/stylesheets/application.scss to have the line:
#import "jquery-ui/autocomplete";
and finally app/assets/javascripts/application.js to have the line:
//= require jquery-ui/autocomplete
more-or-less as described in https://github.com/joliss/jquery-ui-rails
When I try to load a page in development, rails complains saying
File to import not found or unreadable: jquery-ui/autocomplete.
Load paths:
/home/dm/contra/app/assets/images
/home/dm/contra/app/assets/javascripts
/home/dm/contra/app/assets/stylesheets
/home/dm/contra/vendor/assets/javascripts
/home/dm/contra/vendor/assets/stylesheets
/home/dm/.rvm/gems/ruby-2.2.1/gems/jquery-rails-4.0.5/vendor/assets/javascripts
/home/dm/.rvm/gems/ruby-2.2.1/gems/coffee-rails-4.1.0/lib/assets/javascripts
/home/dm/.rvm/gems/ruby-2.2.1/gems/angularjs-rails-1.4.8/vendor/assets/javascripts
/home/dm/.rvm/gems/ruby-2.2.1/gems/bootstrap-sass-3.3.6/assets/stylesheets
...
Here's what the directory layout of the jquery-ui-gem looks like on this machine:
contents of ~/.rvm/gems/ruby-2.2.1/gems/jquery-ui-rails-5.0.5/
app Gemfile History.md lib License.txt Rakefile README.md VERSIONS.md
cocntents of ~/.rvm/gems/ruby-2.2.1/gems/jquery-ui-rails-5.0.5/app/assets/javascripts/jquery-ui
accordion.js datepicker-da.js datepicker-fr-CH.js datepicker-ka.js datepicker-nn.js datepicker-ta.js effect-bounce.js effect-transfer.js
autocomplete.js datepicker-de.js datepicker-fr.js datepicker-kk.js datepicker-no.js datepicker-th.js effect-clip.js menu.js
button.js datepicker-el.js datepicker-gl.js datepicker-km.js datepicker-pl.js datepicker-tj.js effect-drop.js mouse.js
core.js datepicker-en-AU.js datepicker-he.js datepicker-ko.js datepicker-pt-BR.js datepicker-tr.js effect-explode.js position.js
datepicker-af.js datepicker-en-GB.js datepicker-hi.js datepicker-ky.js datepicker-pt.js datepicker-uk.js effect-fade.js progressbar.js
datepicker-ar-DZ.js datepicker-en-NZ.js datepicker-hr.js datepicker-lb.js datepicker-rm.js datepicker-vi.js effect-fold.js resizable.js
datepicker-ar.js datepicker-eo.js datepicker-hu.js datepicker-lt.js datepicker-ro.js datepicker-zh-CN.js effect-highlight.js selectable.js
datepicker-az.js datepicker-es.js datepicker-hy.js datepicker-lv.js datepicker-ru.js datepicker-zh-HK.js effect.js selectmenu.js
datepicker-be.js datepicker-et.js datepicker-id.js datepicker-mk.js datepicker-sk.js datepicker-zh-TW.js effect-puff.js slider.js
datepicker-bg.js datepicker-eu.js datepicker-is.js datepicker-ml.js datepicker-sl.js dialog.js effect-pulsate.js sortable.js
datepicker-bs.js datepicker-fa.js datepicker-it-CH.js datepicker-ms.js datepicker-sq.js draggable.js effect-scale.js spinner.js
datepicker-ca.js datepicker-fi.js datepicker-it.js datepicker-nb.js datepicker-sr.js droppable.js effect-shake.js tabs.js
datepicker-cs.js datepicker-fo.js datepicker-ja.js datepicker-nl-BE.js datepicker-sr-SR.js effect.all.js effect-size.js tooltip.js
datepicker-cy-GB.js datepicker-fr-CA.js datepicker.js datepicker-nl.js datepicker-sv.js effect-blind.js effect-slide.js widget.js
What am I failing to do right here?
You seem to be following the wrong README file. The one you are reading, is for Rails 5.0 and above.
Follow this doc for Rails versions before 5.0 - https://github.com/joliss/jquery-ui-rails/blob/v4.2.1/README.md
I've struggled with this in the past.. Just remove the /autocomplete part and leave the require and import to just jquery-ui and it will load the autocomplete by itself and the rest.
Got my autocomplete working with this coffeescript
jQuery -> $('#booking_product').autocomplete
source: ['']

Resources