Rails 3 assets pipeline js error? - ruby-on-rails-3

I have all the javascript files under app/assets/javascripts
Some of the javascript files
application.js
effect.js
rails.js
prototype.js
scriptaculous.js
When pre compiling assets rake assets:precompile it creating application.js in public/assets
But the application.js file included only the content present in app/assets/javascript/application.js ,not including other js file contents(efect,prototype).
If I add javascript files to config.assets.precompile like this
config.assets.precompile += %w( rails.js menu.js )
this create separate js file for each in public/assets
In my production.rb file
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
I need to combine all javascript files into singlefile.
I'm using prototype.
Anything I am missing?

On application.js you have to include the files you want to combine adding this in the beggining:
//= require effect
//= require rails
//= require prototype
//= require scriptaculous
or if they are all in the same folder
//= require_tree .
Then in your application you must include tags only for application.js, not for each one.

Related

how to include js functions in bootstrap-sass gem

I am relatively new to rails, I found this gem bootstrap-sass which seems to be very nice in the layout. But I want to make the Carousel js plugin work. I found it was documented in the website but I don't quite understand where I need to put
// Loads all Bootstrap javascripts
//= require bootstrap
Thanks,
If you're using rails >= 3.1, add that line to your application.js file (or whatever file you're including in your layout), normally located in app/assets/javascripts
This is my typical base app/assets/javascripts/application.js in any new app.
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require bootstrap

Sprockets - how to require subdirectory in vendor/javascripts

I've some javascript library that I put in the vendor/javascripts, some of them have more than one files so I split them into directories, like:
-- vendor
-- assets
-- javascripts
-- jquery-zAccordion
-- jquery-file-uploader
And I would like to require the directory jquery-zAccordion and jquery-file-uploader in my application.js and I found: https://github.com/sstephenson/sprockets/issues/183#issuecomment-2007808.
I would like to ask, it has been 5 month, is there a solution for this or still we have to use the workaround of using a proxy file?
You can include the following in your application.js file:
//= require_tree ../../../vendor/assets/javascripts
And also for the records, you can do the same for stylesheets in application.css:
*= require_tree ../../../vendor/assets/stylesheets
I guess you need to extend the assets path like this in your config/application.rb file
config.assets.paths << "#{Rails.root}/vendor/assets/javascripts/jquery-zAccordion"
#RyanBigg correct me if i am wrong..
This should do it:
//= require_directory ./jquery-zAccordion

Placing Jquery no Conflict in Rails 3.1.3 Assets

My application is using both prototype and jquery and all files are being getting load by application.js file but i need to add jquery.noconflict(), how can i handle that and where to add this noconflict line.
By thinking some time got its solution. Beside of using noconflict of jquery i was also require to load files with particular order so they dont get conflict.
For this i overwrite application.js file and list down all required js files in required order and one of the js was having jquery.noconflict line to make jquery and protype both compatible.
like
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require noconflict
//= require prototype
//= require compose
//= require s3_upload
and remove the last line require_tree so assets compiler does not add files by its own.
Thanks every one who look in to this question.

Rails javascript include only specific page

Is there a way to include only javascript src I want to a specific page instead of the layout page (application.html.erb)
In your rails project you have
app/assets/javascripts/application.js
which probably contains this line
//= require_tree .
The require_tree word means to include all your JavaScript files to project.
So just remove it and put files that you need:
//= require your_js_file

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.. :)