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 Webpacker #190

Open
redhoodie opened this issue Jun 9, 2019 · 3 comments
Open

Rails Webpacker #190

redhoodie opened this issue Jun 9, 2019 · 3 comments

Comments

@redhoodie
Copy link

redhoodie commented Jun 9, 2019

(This more a n00b question than an issue)
How do I use this with webpacker?

What is the equivalent of:
//= require materialize

Could you please update the Usage section in Readme.md?

@redhoodie redhoodie changed the title Rails 6 Webpack Rails Webpacker Jun 9, 2019
@wuworkshop
Copy link

@redhoodie Was wondering the same thing. I think this Medium article does a good job of explaining it:

https://medium.com/@guilhermepejon/how-to-install-materialize-css-in-rails-6-0-0-beta2-using-webpack-347c03b7104e

@Jmarques
Copy link

Is it me or since this is an engine this is not compatible with Webpacker?
So new default rails app can't use this gem.

@aurora-a-k-a-lightning
Copy link

hello :)
i think i found a way to use the javascript of this gem in a default rails 6 app.

  1. enable webpacker to use .erb via rails webpacker:install:erb. this will add the line environment.loaders.prepend('erb', erb) to config/webpack/environment.js
  2. rename app/assets/javascript/packs/application.js -> app/assets/javascript/packs/application.js.erb
  3. import materialize.js from the materialize gem into application.js.erb via ruby tags, so the file will look like this

(from a brand new rails app)

// 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"

Rails.start()
Turbolinks.start()
ActiveStorage.start()

// add this line at the bottom! be sure to place any materialize.js api calls after this is loaded
<%= File.read(File.join(Gem.loaded_specs['materialize-sass'].full_gem_path, 'assets', 'javascripts', 'materialize.js')) %>

Now we can use the materialize javascript api like in the documentation!

document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('.sidenav');
    var instances = M.Sidenav.init(elems, {}); // M is now defined :)
});

resources:
https://edgeguides.rubyonrails.org/webpacker.html
rails/webpacker#57 (comment)
nathanvda/cocoon#452 (comment)

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