How to configure Rails 3 for JavaScript development - ruby-on-rails-3

I'm moving an app from Rails 2 to Rails 3. I have a bunch of JavaScript files in the app, most of which are for working with Google Maps. Previously, my JS files were in public/javascripts/*.js. I noticed that they were not being loaded by any of the pages that used javascript_include_tag, which writes a JavaScript tag that tries to load the JS file from /assets/*.js. (e.g. /assets/application.js).
I read that Rails 3 expects JS files to be located in app/assets/javascripts/*.js. So I moved my JS files there, but they still won't be accessible at /assets/application.js, unless I run bundle exec rake assets:precompile first.
I can't have my JS development cycle be:
Make change to JS
Run bundle exec rake assets:precompile
Reload browser
Thats just too long. My question is how can I configure my app so that the development cycle is as follows?
Make change to JS
Reload browser
So far I've tried:
Enabling the line that reads "Bundler.require(:default, :assets,
Rails.env)" in config/application.rb
Setting config.assets.enabled = false (and true) in config/application.rb
Thanks in advance.

The Rails asset pipeline should be used with a manifest file. This allows Rails to concatenate all of your javascript when in production. In order to do this, you should have an application.js file that looks something like.
//= require jquery
//= require jquery_ujs
//= require_tree .
The important part here is the require_tree part. This tells Rails to include all of the other javascript files in the same folder. This means that you want both the manifest version of application.js and all of your custom javascript files to live in app/assets/javascripts.
A good guide for transitioning to the asset pipeline from Rails 2 can be found in this RailsCast: http://railscasts.com/episodes/282-upgrading-to-rails-3-1

recently i have this issues
I change file config/environments/development.rb
config.assets.digest = false
then that working

Related

Access wrapper app assets file to engine in rails3

I am creating a rails engine inside a rails app. I have a test.js file in rails app and I want to include this file in my rails engine's application.js file.
My engine is in vendor/engines folder.
How can I include wrapper(main) app's assets file into engine?
I am new for rails engine. If I miss something please let me know.
If I am getting you right then you want some DRY code for using same js file in different places.
Rails starts looking from the root directory. So you just need to require the js file same as you did in application.js.
For example
//= require test

Asset pipeline on Heroku not loading dependancies

I've got a Rails 3.2.8 application which appears to be working nicely in my development environment however when deployed to Heroku the asset pipeline is no longer compiling my assets correctly - or at least, not loading in my dependencies.
The resulting 'compiled' js asset for instance which is found on http://myappsubdomain.herokuapp.com/assets/application-b389f6c8ee6250ebc143feb659f40ed9.js contains the raw source code:
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .
$(".tip").tooltip();
This is also true of my CSS assets, which include any styles placed directly in my application.css, but with none of the required dependant assets loaded in.
The precompile seems to run during deployment without error:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (8.79s)
I notice that if I manually run "heroku run rake asset:precompile" I runs without error, only a couple of depreciation notices.
If I enabled asset compilation in my development environment everything appears to be squashed in to the single included asset just fine, as I would expect.
Can anyone offer any advice on this?
After some more experimentation I noted that the new Heroku instance I had provisions was running on Ruby 2 (obviously in prep for Rails 4) but my development environment and existing heroku instances running this given code base were on 1.9.3.
I have updated my Gemfile to include the explicite definition of the Ruby version and things now appear to work wonderfully when deployed.

rails 3.1 upgrade with backbone, can't find ujs and remotipart files

I upgraded to rails 3.1.3 a few weeks ago, and moved a bunch of stuff around to use backbone.
Mostly, things are going well, but I just realized that the rails-ujs.js file and remotipart.js files are not in the assets directory.
I've looked for them in the old public/javascripts folder, as well as everywhere else, but no luck finding them.
I also can't seem to find the files in the git repos. Is there somewhere to download these files in Javascript? or do they need to be generated by rails? Is there a way to regenerate them?
---------------this could be my error ----------------------------
I just realized that with the assets packed, you can't just open a file from view source, they all show as non-existent.
I think theses files lived in the rails ujs plugins in asset directory. so you need to add it to your Gemfile and the asset pipeline should see them automaticaly.
all files here https://github.com/rails/jquery-ujs/tree/master/src are available to include in your js file with
//= require jquery
//= require jquery_ujs
see plugin readme for more informations
You need to include this in your Gemfile:
gem 'jquery-rails'
That is the official plugin for this code.
Run bundle install.
Then you add the lines:
//= require jquery
//= require jquery_ujs
to you application.js file.

Rails 3.1 precompiled assets breaking javascript remote_forms in production mode

I've fixed all the errors about "xxx" is not compiled and all the assets show up -- when running locally everything works fine:
All ajax requests work
Form submissions use the rails remote tag and fire off properly
However when running in production mode locally (and on Heroku):
Some ajax will work -- however things like PUT's that should be updatating records (and do in dev) don't... They will hit the page but not do the actual database update
Remote forms are completely broken and resulting in regular form submission
The source can be cloned from here: https://github.com/bluescripts/reru_scrum
Maybe I'm miscompiling the assets wrong or maybe I'm missing an appropriate include in my application.js file?
I've been compiling via:
rake assets:precompile
You're missing //= require jquery_ujs in your application.js. This file comes with jquery-rails gem and is responsible besides other things for handling remote links and forms.
Btw, I'd suggest removing .Gemfile.swp from your repo and adding .*.swp to .gitignore.

What is the purpose of config.assets.precompile?

In Rails 3.1, you must whitelist files that you want included in asset precompilation. You must open up config/environments/production.rb and explicitly include assets you want precompiled:
config.assets.precompile += ['somestylesheet.css']
If you don't do this this and you run rake assets:precompile, your asset will not be copied to public/assets, and your app with raise an exception(therefore causing a 500 error in production) when an asset is not found.
Why is this necessary? Why aren't all assets automatically precompiled?
This current approach creates extra code and stress when deploying. Wouldn't it be easier to blacklist/exclude assets so things worked right out of the box? Anyone else share these feelings?
Most assets are automatically included in asset precompilation. According to the RoR Guide on the Asset Pipeline:
The default matcher for compiling files includes application.js, application.css and all files that do not end in js or css: [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
You would use config.assets.precompile if you have additional assets to include:
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
Or you could overwrite it.
I think it has to do with the pipeline/sprockets ability to require separate files.
For example, I have an admin.js file in my app/assets/javascripts folder. But all it does is require several other .js files.
//= require jquery
//= require jquery_ujs
//= require jquery.colorpicker.js
//= require jquery.wysiwyg.js
//= require wysiwyg.image.js
//= require jquery.fileupload.js
//= require jquery.fileupload-ui.js
//= require codemirror.js
//= require css.js
//= require admin_load
This is because (a) I'm using external js plugins and (b) I like to keep things like jQuery onload handlers in separate files.
If every .js file was precompiled, then it would precompile each one of these individual files–which is totally unnecessary. All I want/need is the single admin.js file precompiled.
Same goes for CSS files.
The assets precompile to me is cool so you dont end up deploying assets that you do not want. Dont also forget about the uglifer gem that helps compress your javascripts. Imaging all this are not existing and you just deploy your app and you find out that you have unused css files and uncompressed javascripts. how would you feel. this is just my own opinion and i say the asset pipeline is the coolest thing in rails.. Being able to manage all your assets properly.
And mind you if i am rails i would not want to compile assets that you would not want so you would say in your mind why did this guy compile these assets.. :)