4

I tried many things to add Materialize CSS to rails 7 . I need some helps to add Materialize to rails 7 . i tried use

--css=materialize
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 06 '22 at 14:34
  • Did you get your answer yet? I cloned the example Rails 7 app from the Materialize GitHub git and will try to use that as the starting point. Looks like there are provisions for Turbo in the init.js – doer123456789 Jul 19 '22 at 11:15

2 Answers2

1

The easy way will be using gem

  1. Add this to your Gemfile
  gem 'materialize-sass'
  gem "sassc-rails"
  1. Run bundle install in Terminal to install new Gems
  $ bundle install
  1. change app/assets/stylesheets/application.css to app/assets/stylesheets/application.scss

  2. Add this line in app/assets/stylesheets/application.scss

  @import "materialize";

  1. add this line in app/config/manifest.js
//= require materialize

Now you can use Materialize in Your App

For more information checkout materialize-sass

Korak
  • 39
  • 8
  • Although CSS might work with this, the JS part of Materialize doesn't. Doesn't it also need jQuery to work? How are you dealing with this in the JS controllers? Your answer is more relevant to Rails 5 than Rails 6 and Rails 7. This question is Rails 7 specific. – doer123456789 Jul 19 '22 at 11:08
  • i just needed to use CSS in VIEWS for basic styles, so i did not really use any JS . Thats why am not really sure how it gonna work in those cases. – Korak Jul 19 '22 at 12:42
1

If you want to use materialize JS functionalities like dropdown, sidenavs, etc. This is the process that worked for me.

You can choose one of two paths. The first path involves using the current materialize-sass gem and you'll need follow the four steps. For the second path you'll only need to go up to step three but you'll be using my fork on Github of the gem in your Gemfile

Use the original gem (Follow all the steps)

gem 'materialize-sass'

Use my forked version of the gem (Follow up to step three)

gem "materialize-sass", git: "https://github.com/Joaquinb2000/materialize-sass-rails-7.git"

After installing one of the gems be sure to follow steps 3 and 4 from Korak's answer to add the materialize stylesheets to your app.

Step 1. Add this line at the end of your config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( materialize.js )

Step 2. Add these two lines after everything in config/importmap.rb

pin "jquery", to: "https://code.jquery.com/jquery-3.6.3.min.js"
pin "materialize", to: "materialize.js"

Step 3. In your app/javascript/application.js put these two at the end

import "jquery"
import "materialize"

After that, remember to add the correspondent code to activate the effects you want. For example, if you wanted to activate dropdowns you'd add the following line after the 'import "materialize"':

$(".dropdown-trigger").dropdown();

Step 4. Find the install path for your gems. In my case I use rbenv in ubuntu wsl, so you'd find it in your home directory "~/.rbenv" or "/home/#{your_username}/.rbenv"

Once you find the location of the gem and you're inside its folder go to "assets/javascripts" and edit materialize.js with the code editor you like best. Go over to line 1509 and replace the following line:

p = $jscomp.global;e = e.split(".");for (m = 0; m < e.length - 1; m++) {

With:

let p = $jscomp.global;e = e.split("."); if(!p) p = new Array();for (m = 0; m < e.length - 1; m++) {

(This fix for materialize.js comes from courtesy of https://github.com/Dogfalo/materialize/issues/6557#issuecomment-1253883870. Thank you)

  • as of Materialize v1.0.0 find it on line 1511 – iChido Aug 15 '23 at 16:31
  • I keep getting this error, even after making the edit you suggested to the materialize.js file: Uncaught TypeError: Cannot use 'in' operator to search for 'Array' in undefined at $jscomp.polyfill (materialize.js:1512:22) at materialize.js:1515:11 Any suggestions? – iChido Aug 15 '23 at 17:22
  • Additional comment to the above: I am on a new Rails 7 app – iChido Aug 15 '23 at 17:28
  • I figured out how to apply my fix by following the steps on this solution https://stackoverflow.com/a/71303435/788322 – iChido Aug 15 '23 at 17:41