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 8472279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:01:14+00:00 2026-06-10T17:01:14+00:00

I’ve been thinking about CoffeeScript lately, and I just upgraded a Rails project I’ve

  • 0

I’ve been thinking about CoffeeScript lately, and I just upgraded a Rails project I’ve been working on to Rails 3.2.8 (from Rails 3.0.9 which didn’t have the asset pipeline) following this guide.

I had to do some hackish stuff to get things to work properly. I’m testing out a new page, ‘/pages/game’ and so it has some example CoffeeScript in the the asset directory.

(app/assets/javascripts/page.js.coffee)

class MyObject
  constructor: ->
  hello: -> alert 'hello world of coffeescript!'


a = new MyObject

a.hello()

Then I added a line to my production environment:
(config/environments/production.rb)

config.assets.precompile += %w( pages.js )    # this is needed to precompile coffee script files... it is difficult to understand how manifest files work...

Then typing

$  bundle exec rake assets:precompile

Worked to compile page.js The only problem is that the class I created with CoffeeScript doesn’t work exactly as I anticipated. So I opened up my developer console in FireFox and attempted to instantiate the class manually, but it acted like there was no such object named MyClass.

So where have I gone wrong? Was it presumptuous of me to assume I could manually instantiate classes I’ve written in CoffeeScript? Was my hackish means of adding pages.js to the precompile array inappropriate? If you’re a CoffeeScript pro, how would you test your classes and such?

Update:
Part of my problem was the ‘variable privacy’ that is inherant in coffee script classes. This privacy can be implemented in standard javascript, and should be understood before dabbling too far into coffee script. http://benalman.com/news/2010/11/immediately-invoked-function-expression/

That said, the coffee script class needed to be attached to the window object to make it globally accessible, like so:

Corrected

class MyObject
  constructor: ->
  hello: -> alert 'hello world of coffeescript!'


window.MyObject = MyObject;

Once the class is published in this manor (by attaching it to window as a global) it can be instantiated via a = new MyObject() and then have its function called normally to alert to the screen a.hello()

  • 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-10T17:01:15+00:00Added an answer on June 10, 2026 at 5:01 pm

    MyObject absolutely exists if you include pages.js‘s contents into your page. You likely need to add

    <%= javascript_include_tag "pages" %>
    

    to your HTML or not set

    config.assets.precompile += %w( pages.js )
    

    and instead add the following to app/assets/javascripts/application.js.coffee

    //= require pages
    

    and make sure application.js is being included in your app/views/layouts/application.html.erb with

    <%= javascript_include_tag "application" %>
    

    The Rails Guide on the asset pipeline is quite helpful. To provide a bit of explanation here though…

    Rails wants you (by default) to pack all of your javascript into a single application.js file when deploying to production. Sprockets is available to let you add require directives like the one above to find, include, and compile the files listed into the application.js file.

    By default Rails will only compile application.js, application.css and any NON-.js/.css file in app/assets, lib/assets, and vendor/assets. This means, unless you add a //= require directive for pages.js to your application.js file, Rails is going to ignore it.

    Thats why you tried adding pages.js to the list of files for compilation with config.assets.precompile. This tells Rails explicitly to compile that file, making it available through a javascript include like

    <%= javascript_include_tag "pages" %>
    

    which I am assuming you’ve not put into your HTML. Going this route is not typically what you for a few reasons, one of which is that by doing so you’re saying “No thanks” to the help Rails is trying to provide you with the asset pipeline //= require lines.

    So, either method is doable; you’ve just yet to follow through with all the steps necessary to get pages.js‘s contents included in your page, which is why MyObject doesn’t exist.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.