Rails app/assets/javascript folder,I dont fount

Hello there.

I’m learning ruby on rails for web programming and now it’s time to manage javascript files. Since the source I learned is a bit old, it says that there is a folder named /app/assets/javascript in my rails application, but there is no such file in my application. I wonder if I need to add this folder myself, I would appreciate if you help me.

Ruby version = 3.0.1

Rails version = 6.0.4

Example ScreenShot:

(This text has been translated by google translate.)

Thanks.

It sounds to me as though this application was upgraded from a previous version of Rails to 6.x. The app/javascript directory was added to the default application template in 5.2, if I recall correctly. If you started with Rails 5 or 5.1 (or even earlier than that) you would not have the directory by default.

You can add that directory yourself. You’ll also want to ensure that if you went through the normal Rails upgrade path, using the task rake app:update to update all your defaults, that the other parts of that tooling are also present and being loaded. To take full advantage of that system, you’ll need to have the webpacker gem installed and configured with its YAML file.

One of the easiest ways to see what is different between an application that was started with 6.x and one that was upgraded to that level is to spin up a new empty app with Rails 6, and then compare the preferences and defaults.

Walter

1 Like

There are now several ways to manage javascript files and depending on how your Rails app was set up, the javascript folder may be somewhere else. This app appears to use Webpacker hence /app/javascript/. GitHub - rails/webpacker: Use Webpack to manage app-like JavaScript modules in Rails which doesn’t acknowledge the /javascript folder. You’ve found a major problem with Rails at the moment. Rails struggles with how to deal with JavaScript. A short history would be Sprockets, Webpacker and now Rails ES6 based replacement for webpacker - #2 by juansecaro, a post written by the creator of Rails, DHH.

Webpack VS Sprockets - The Lean Software Boutique will give you an idea of what’s involved. Since Webpacker is relatively new people have different approaches on how to implement it and you’ve discovered that. Many good posts and YouTube tutorials have been done about Webpacker in Rails, but they all seem to take slightly different approaches and the official documentation hasn’t kept up.

I don’t know Rails that well but did get caught up in the Webpacker a year or two ago. And maybe my naive post will generate other posts that will help you.

Good luck.

1 Like

@MtnBiker Thank you so can I write my javascript files to /app/javascript hardware? Can you suggest a source?

I don’t understand the question. I assume “hardware” is really “folder” or “directory”.

But what “source” are are you asking about?

1 Like

Webpack.