Skip to main content

Organising per view javascripts

Rails • Asked by Luca Rossi
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!





Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.