Techie April 2022
Introduction
Creating a Rails project is as simple as running one line of command on the terminal. While it’s easy to do so, it’s imperative to configure the basic tools for the project. This section outlines the necessary steps for creating and configuring a new Rails 7 project. We will use esbuild for JavaScript bundling, bootstrap for CSS and JQuery for JavaScript and SQLite as the database.
Prerequisites
- Ruby 3.2.0
- Rails 7.0.0 or higher (Rails 7.0.2.3 was used at the time of writing this document)
- NPM version 8.6.0 (or higher, nmp 8.6.0 was used at the time of writing this document). You can update npm by running: sudo npm install -g npm@latest or sudo npm install -g npm
- Yarn
Creating the project
1 . Open the terminal, cd into your projects directory and run this command:
The -j directive is used to define the JavaScript bundler while the - -css directive defines the CSS library.
If you don’t want to use the default SQLite database, you can specify a different database with the -d directive like so:
Even after the command has created the project, it is possible to switch
databases like so, in root of the project:
NB: The bootstrap version installed will be the latest version.
2 . cd into my_app and add jquery, jquery-ujs and jquery-ui
3 . Import jquery, jquery-ujs and jquery-ui in app/javascript/application.js
Normally, this is how we would import them.
But since JavaScript imports are hoisted; i.e. all imports are loaded before any other code regardless of the order, it causes a problem because JavaScript will try to load jquery-ui before jquery is made available to the window. Let’s fix that:
i). In app/javascript, create src sub-directory and in it create jquery.js file:
Add this to it:
ii). In app/javascript/application.js add this:
4 . Set up notifications
i). Add notifications in app/views/layouts/application.html.erb
ii). Create the flash_class method in app/helpers/application_helper.rb
5 . Create a navbar in app/views/layouts/application.html.erb
6 . Create The Home Page
i) Generate the controller and view
ii) Open the home page in app/views/home/index.html.erb and add this code
ii) Create home.js file in app/javascript/src and add this jquery code.
iii) Import home.js in app/javascript/application.js
iv) Set home page as the default route in config/routes.rb
Testing the app
To run the app, cd into the root of the project and issue this command:
Now navigate to localhost:3000. The two buttons should be working if you’ve made the configurations described here. If you wish to change the default port, open the Procfile.dev file located in the root of the project and change the port number.
Something to remember
If you are going to use inline or embedded javascript in your app pages, jquery will not work.
For jquery to work, in app/views/layouts/application.html.erb, set defer: false in the javascript_include_tag. If the option is set to true, the browser will throw this error Uncaught ReferenceError: $ is not defined.
NB: Defer loads javascript last to prevent it from blocking app content from loading. Therefore, defer: false can prevent the app from loading smoothly i.e makes the page appear to freeze halfway through the rendering. You’re better off using external scripts.
We have covered the necessary configurations for the project. If you run the app, you should now be able to use jquery and bootstrap to build the project.
Thanks for reading, see you in the next one!