Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 6, turbolinks 5 Cannot find module 'cookies_eu' #85

Open
martinbarilik opened this issue Oct 2, 2020 · 4 comments
Open

Rails 6, turbolinks 5 Cannot find module 'cookies_eu' #85

martinbarilik opened this issue Oct 2, 2020 · 4 comments

Comments

@martinbarilik
Copy link

Gem doesn't work with latest version of RoR

rails g cookies_eu:install

helper doesn't work as well, it expects application.js to be in /app/assets/javascripts/application.js

and generates error

/app/assets/javascripts/application.js does not appear to exist

After that, when loading page, banner can't be dismissed because of

application.js:1 Uncaught Error: Cannot find module 'cookies_eu'
    at webpackMissingModule (application.js:1)
    at Module.<anonymous> (application.js:1)
    at Module../app/javascript/packs/application.js (application.js:41)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83
@gabskoro
Copy link
Collaborator

gabskoro commented Oct 2, 2020

RoR 6 is using webpack now and therefore this can't work anymore. We could also create the folders on our own, but not sure if this is what we want.

But what you could do is...

  1. Create /app/assets/javascripts/application.js on your own
  2. Run rails g cookies_eu:install
  3. Add <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> to the application.html file
  4. Add //= link_directory ../javascripts .js to the manifest.js file

@martinbarilik
Copy link
Author

Naah, not working as well. Still complaining about module cookies_eu not being found.

Here is everything you need to reproduce.

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.2'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'devise', '~> 4.7.2'
gem 'pg', '~> 0.18.4'
gem 'whenever', require: false
gem 'cookies_eu'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'puma', '~> 4.1'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

application.js

// 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("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()

import 'bootstrap';
import 'bootstrap-table';
import 'bootstrap-table/dist/bootstrap-table-locale-all.min';
import 'bootstrap-table/dist/extensions/cookie/bootstrap-table-cookie.min';
import "@fortawesome/fontawesome-free/js/all";

aplication.html.erb HEAD

<head>
	<title>Some name</title>

	<meta name="robots" content="noindex, nofollow">

	<%= csrf_meta_tags %>
	<%= csp_meta_tag %>

	<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
	<%= stylesheet_link_tag 'application', params[:controller] %>

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

Feel free to ask for whatever else you need.

@gabskoro
Copy link
Collaborator

gabskoro commented Oct 6, 2020

As I see you only have the application.js from the pack folder. Do you also have one under /app/assets/javascripts/application.js? In there you will need to require the //= require cookies_eu js code. Also, the //= link_directory ../javascripts .js in the manifest file makes sure the code gets compiled.

So what you need to do is...

app/views/layouts/application.html.erb

image

app/assets/javascripts/application.js

image

app/assets/config/manifest.js

image

And for the rest continue using the app/javascript/packs/application.js file where you will include the JS code from packages which were included through node_modules (yarn, npm...)

@cseelus
Copy link

cseelus commented Oct 24, 2020

Unfortunately this only works if you still have Sprockets as a dependency of your Rails app installed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants