Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:51+00:00 2026-05-23T16:20:51+00:00

When writing javascript for a specific page in a site, when do you want

  • 0

When writing javascript for a specific page in a site, when do you want to turn the javascript into a function and include it in application.js?

I’ve seen suggestions about doing this (and minifying or gzip-ing) to minimize HTTP requests. That makes sense, but what about maintainability? If I have js code specific to one view, it seems like more work to look into a potentially massive application.js. That code could be embedded into that view or put in its own .js (or .js.erb or .rjs) file in that view folder.

I’ve seen another suggestion that Rails automatically merges all javascript into one file. Is this true?

TLDR: how much or how little should a developer worry about optimization when writing javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:20:52+00:00Added an answer on May 23, 2026 at 4:20 pm

    As I haven’t seen an answer in about a month, I’ll answer this question to the best of my current knowledge.

    Rails 3.1 (currently at release candidate 4) introduces sprockets, which will compile all javascript in a rails project into one file. It even comes with tools to minify and compress javascript so that it’s all delivered to the client at once.

    Related to sprockets is the Rails 3.1 asset pipeline. As I understand, it’s a folder hierarchy/abstraction. Javascripts can go into 3 folders:

    /apps/assets/javscripts/
    Javascript files specific to the application, including application.js. This should only contain the manifest of javascript files you want to include in your project. The rails new tool will generate this file and include jquery in the manifest.

    /lib/assets/javascripts/
    Javascript files written by the developer that are more general purpose. (My impression is that this would be for javascript libraries you develop to drop into multiple applications)

    /vendor/assets/javascripts/
    Third party javascript files (i.e. JQuery, Modernizr)

    All files in these folders will appear to the client inside /assets/, abstracting out the server side file paths. I assume this is meant to help the developer organize javascript files.

    To answer my own question

    • Put javascript functions into separate files, group them logically. My test app indicated that subfolders within .../assets/javascripts/ are ok if and only if the subfolder path is included in the manifest.
      • I.E. putting //= subfolder/js_file in the manifest will work. Putting //= js_file will not if js_file is inside .../javascripts/subfolder/
      • DHH mentions a “rule of 13” in his talk (linked below). If the number of javascripts in a folder exceeds 13, it starts to look messy and unmanageable. This would be a good time to group javascript files into subfolders.
    • Use the built in Rails 3.1 minifier and compressor (or install a preferred gem)
    • Refactor javascript code from time to time. Move functions to /lib/assets/javascripts/ over time. (The goal is to eventually recognize when you want to write general purpose javascript functions as opposed to application-specific ones and eliminate this refactor step)

    More Resources

    a blog post covering all changes in Rails 3.1
    DHH’s talk on Rails 3.1 changes, May 16 2011 (~1 hr)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function I'm writing which is being used to include an
In writing javascript based apps, I'm often finding that I want to include information
I'm writing some Javascript to resize the large image to fit into the user's
I am writing a JavaScript function to highlight the row and column of a
I'm writing a Javascript function that would manipulate an array written on-the-fly and sent
I'm writing some Javascript code and I'm trying to change the current page as
I was using javascript to detect for specific key strokes and while writing the
I'm testing a JBoss / Tomcat web application, writing JavaScript tests in .js files.
I'm writing a simple Javascript to add a specific parameter to a specific template
I'm writing a regular expression in javascript that replaces whitespaces except when: Some specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.