Requirements to use Paloma gem
- jQuery 1.7 or higher
- Rails 3.1 or higher
Paloma is a very beautiful library through you can load the JS as per the page and its action. Its setup is less time taking on the other hand the performances is very high.
1. Add the gem to gemfile
sudo gem install paloma
With bundler, add this to your Gemfile
2. Require Paloma in your application.js file
//= require paloma
3. Add the Paloma hook to your layout file
In the current application, I am using application layout so I am adding Paloma’s hook (insert_paloma_hook) to application.html.erb file.
4. Start Paloma to initialize the appropriate controller and execute a certain action.
This is optional you can add the below code in the application.js file as well it totally depends on you.
After this, you need to require the initializer.js file to your application.js file after the Paloma
Now you are all set and ready to go with Paloma.
Let's talk about how it works with Rails controller and its action.
It is accessed, using Paloma.controller method if exist.
To handle specific actions of your Rails controller, you need to add methods to your Paloma controller's prototype.
You can also add the Paloma for the namespaced controller
Let’s starts with an example
Currently, I am having one simple controller named clients controller, I need to load the JS code only for the index action of the client so when the client index action is fire at that time that specific JS is load.
Note: Keep in mind that you need to require your specific JS to application.js file for making it available for the controller or its action when any specific page is load.
So whenever I hit the 'http://localhost:3000/clients' URL in the browser it will give me an alert message says ‘This is the JS code for clients index action’
But this alert is only displayed in the client's index action. In the other pages, it will not load.
Client Index Page
You can also define the multiple actions in single Js file
In clinet.js file, you can define multiple actions in that single file like,
So this depends on you how you want to make your JS folder structure and how you want to use it into your code.
This is how you can load the page specific JS to your application as per your requirements and make your application run faster than the usual. Reach out to learn more about the expert software development companies in NYC for the various ways to improve or build the quality of projects and across your company.