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

  • Home
  • SEARCH
  • 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 8918233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:42:04+00:00 2026-06-15T05:42:04+00:00

I have several javascript (coffee script) files in my Rails javascripts directory. The code

  • 0

I have several javascript (coffee script) files in my Rails javascripts directory. The code is logically divided into different files. However, each file starts with $(document).ready -> and some files share common helper functions.

What is the best way to factor out the helper functions? Should I just put them all in some other file that gets included earlier in application.js?

Also, is it normal to have the code divided up the way mine is, where every page does $(document).ready ->? Doesn’t this mean that all of my code is called on every page, regardless of whether or not it is relevant? Are there alternatives to this organization?

I’m not sure if this question is specific to Rails or relevant to javascript and jQuery in general.

  • 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-06-15T05:42:05+00:00Added an answer on June 15, 2026 at 5:42 am

    I do think this is a Rails question, and a good one.

    The normal paradigm in Rails, where “global” stuff goes in application.* is a little messed up with the asset pipeline, since the application.js really acts as a manifest, rather than a common file. Of course you could add stuff there, or even create an application.js.coffee for your common code. I decided to create a file called common.js.coffee (and in another case shared.js.coffee), which in my case was automatically handled by the require_tree . directive.

    (Update based on comment from @jonathan-tran) In some cases, you may just want methods called on document ready for all pages — for example, I used this to make a datepicker available to any field in any view. If, instead you want methods (actually global functions) available to be callable, you’ll need to export the coffeescript functions to variables, for example by attaching to the window object. You can do both in a shared file.

    It is true that if you use generators you’ll end up with files for every controller which, if there’s no specialized code result in a series of redundant $(document).ready -> statements when assets are compiled. So just get rid of the ones you don’t use. But following the pattern of separating functionality specific to a page makes good sense to me and works well — I know where to look to find stuff and that’s worth a lot as a project grows.

    And another rule I have learned with Rails: go with the flow. If that’s how Rails does it, it’s probably a good way. They do indeed think about these things. Don’t fix what works 🙂

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

Sidebar

Related Questions

We have several fairly large JavaScript files embedded into a single script resources DLL.
I have several javascript files and I'd like to combine them into one file
Just wondering about this... I have several separate javascript files, that all contain code
I have several javascript files that contain different parts of my node application. They
I have several large Javascript files that I need to document/digg into. Unfortunately I
I have a situation where I'm trying to consolidate several Javascript files into one,
We have several JavaScript files which we load at the bottom of the master
I have several xml files with different node structure. I want to extract xml
I have several javascript files that during run-time get combined and minified. This is
Say I have several JavaScript includes in a page: <script type=text/javascript src=/js/script0.js></script> <script type=text/javascript

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.