All threads / Organising per view javascripts

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Organising per view javascripts

Luca Rossi asked in Rails
Hi all,

I am refactoring my app since it is the first one and I have been doing the wrong things :)

This topic is about javascript assets. I have moved all the vendor libraries to the vendor folder and swapped min files with normal since rails does compress all.

I was also including page specific .js files in view depending on the libraries I needed and I have moved the bulk back to application.js as I noticed this was not the best practice.

Now I am dealing with view specific javascript. I am using this approach from Brandon Hilkert: controller and action name in body tag and return unless the relevant controller/action are shown.

Almost every controller index uses bootstrap table to list model items. These tables are initialised with javascript and some of the code such as action buttons and columns differ. Also the new/edit action for the same controller has specific javascript handling different features.

Following the above approach I could either create a separate file for each controller action (i.e. one for index and one for edit for each controller).

Is this a good approach or would you recommend something else?

Thanks!




Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,629+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.