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

Javascript Insert in Rails 6 #140

Closed
ZephiroRB opened this issue Sep 27, 2019 · 6 comments
Closed

Javascript Insert in Rails 6 #140

ZephiroRB opened this issue Sep 27, 2019 · 6 comments

Comments

@ZephiroRB
Copy link

How to include javascript in Rails 6?

@doabit
Copy link
Owner

doabit commented Sep 27, 2019

In rails 6, you can still use app/assets/javascripts/application.js

//= require semantic-ui

or use with webpacker https://github.com/rails/webpacker#resolved, but i recommend to use yarn add semantic-ui,

@jlebrijo
Copy link

jlebrijo commented Nov 5, 2019

I am having the same problem:

yarn add semantic-ui is not a good idea because it is not automated, this is asking for a Gulp prompt that will break the production deployment in the future.

//= require semantic-ui raises the following error:

Uncaught ReferenceError: jQuery is not defined

Maybe this should be referenced in app/javascript/packs/application.js in order to find JQuery. But how?

Can you help, please?

@doabit
Copy link
Owner

doabit commented Nov 6, 2019

@jlebrijo You can try to put <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> before javascript_include_tag.

@jlebrijo
Copy link

jlebrijo commented Nov 6, 2019

Great point @doabit , thanks!

Finally I could integrate Rails 6 with this gem. Just sharing my complete solution.

We have JQuery as prerequisite: yarn add jquery popper.js

After that we have to create $ and jQuery handlers into webpack config, at packs/application.js:

import JQuery from 'jquery';
window.$ = window.jQuery = JQuery;

Finally we have to import all files in the correct order at views/layouts/application.html.haml:

= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'

Then we can do the usual imports. CSS at assets/stylesheets/application.scss:

@import "semantic-ui";

And Javascripts at assets/javascripts/application.js:

//= require semantic-ui

@AlexanderCohen
Copy link

AlexanderCohen commented Feb 2, 2020

Hey @doabit, I believe the Javascript file has moved in rails 6 from app/assets/javascripts/application.js to app/javascripts/packs/application.js

Edit: The above solution works for me - however it will require you to include the //= link application.js in the app/assets/config/manifest.js

Related to this comment:

In rails 6, you can still use app/assets/javascripts/application.js

//= require semantic-ui

or use with webpacker https://github.com/rails/webpacker#resolved, but i recommend to use yarn add semantic-ui,

albertski pushed a commit to albertski/semantic-ui-sass that referenced this issue Mar 19, 2020
doabit added a commit that referenced this issue Mar 19, 2020
#140 Adds instructions for installing javascript in Rails 6
@doabit
Copy link
Owner

doabit commented Jun 24, 2021

@doabit doabit closed this as completed Jun 24, 2021
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

4 participants