I want to stay DRY in my code so I want to auto-load my javascripts file when it matches a controller or/and a method and the .js exists. I added this to my layout
= javascript_include_tag params[:controller] if ::Rails.application.assets.find_asset("#{params[:controller]}.js")
= javascript_include_tag "#{params[:controller]}/#{params[:action]}" if ::Rails.application.assets.find_asset("#{params[:controller]}/#{params[:action]}.js")
So now when I add javascripts/my_controller/my_method.js it automatically loads it, which's nice.
Sadly I must add another line to precompile the asset otherwise an error is thrown (which says I must precompile my .js file) and I didn't find any way around this.
Rails.application.config.assets.precompile += %w( orders/checkout.js )
Does anyone has a solution to avoid tu add manually elements in this configuration ?
NOTE : I already tried to use require_tree . which was just loading all the files on every page and was not working in my case.
You can use a wildcard to allow all JS files included in your views to be precompiled:
config.assets.precompile << '*.js'
Related
Please enlighten me!
Let say I have the following assets structure in a Rails 5 application.
app/assets/javascripts
application.js
file1.js
In the application.js manifest I am not requiring //= file1 neither I am using require_tree . directive.
If a JS file is not required in any way in the manifest application.js neither is it included in the Rails.application.config.assets.precompile += %w[]
Is this asset going to be served?
If there is a tag in some view <%= javascript_include_tag 'file1' %> , should it work or raise an error?
No, that file is not served by default. By default (depending on Rails version/asset config), only the files listed in Rails.application.config.assets.precompile, by default application.js application.css or in newer Sprockets/Rails via files listed in manifest.js are available for direct serving via javascript_include_tag.
That 2. would raise an error.
I'm having an issue when it comes to requiring individual Javascripts for Jasmine file. My application.js.coffee file doesn't include require_tree as I am loading page specific javascript in a tag in each view.
I added this:
src_files:
- assets/application.js
- assets/*.js
which does not seen to load any JS files that are located in assets/. However, the files are clearly there because if I use
src_files:
- assets/application.js
- assets/feedback.js
The feedback.js file is loaded correctly and the test passes. Why does the wildcard not load the files in directory? Also assets/*/.js doesn't work either for folders.
When the jasmine gem looks up your files on disk it uses Dir.glob to expand out the wildcards (*) in the path. In the case that Dir.glob doesn't find any files, and the path specified doesn't have any wildcards in it, then jasmine will just add that file anyways. This basically assumes you're using sprockets and this will work.
This means, if all of you top-level includes are coffescript files they won't actually match *.js.
You can either:
Add an additional top-level .js file that includes all of your javascript and use that in jasmine.yml
Add each of your top-level files to jasmine.yml individually
Add a *.js.coffee rule to you jasmine.yml and then a negative matcher to remove things you don't want.
The last option would look something like this (the ! tells jasmine to remove files matching this glob from the list of files to include):
src_files:
- assets/application.js
- assets/*.js.coffee
- !assets/subfolder/*.js.coffee
I've decided to use javascript_inlcude_tag 'google_analytics.js' in my templates, avoiding many of the gems that would get it done for me in favor of testing the new Google Analytics "Universal" features.
Thing is that Google Analytics provided code is not in Coffeescript, and so I add it to my vendor/assets/javascripts just like I've done with many other libraries used in my App. But unlike all the others, the javascript_include_tag approach is causing Rails to halt on the error:
Rails ActionView::Template::Error (google_analytics.js isn't precompiled)
The other vendor libraries are just being 'required', inside many of my Coofeescripts.
Does anybody know what is wrong with this current analytics javascript approach?
Likewise suggested by Dan McClain in this (related) post, the issue was solved by adding the following line in config/environments/production.rb:
config.assets.precompile += %w( google_analytics.js )
Done!
A previous comment:
Google Analytics provided code is not in Coffeescript, and so I add it to my vendor...
It is not necessary for the js to be in Coffeescript to be either included in your assets pipeline through a direct //= require google_analytics in application.js, being the extension of the file .js.coffee or just .js.
Said that and assuming that you are getting that error in the production environment it seems that you are not adding your js file to the assets precompile array. In your config/environments/production.rb the following line is needed:
config.assets.precompile += %w( google_analytics.js )
Hope that helps
I have a coffee script user.js.coffee, that is only used in certain views. I achieved this by using the following answer:
https://stackoverflow.com/a/6795533/784318
Now I have excluded the script from the application.js. I also removed the //= require_tree . entry.
So my file is available here: http://localhost:3000/assets/user.js however, when I deploy this to the server the assets will be combined in one application.js so how can I make sure that the user.js will be available on production like so: http://myserver.com/assets/user.js?
In environments/production.rb (or the environment you need precompile to occur) uncomment or add this file to the precompile array:
# environments/production.rb
config.assets.precompile += %w( user.js )
Other entries might be already present, just add any other file that you need to access separately.
This file will not get compiled in one big application.js file and will be accessible separately as user.js
You can try putting your user.js file to the public folder of your application directly and configure your asset pipeline to exclude it from the "zipping" process.
I'm using Rails 3.2, It's set up for coffeescript. I know Coffeescript is an awesome language and it's not too hard to learn, but i'm JUST starting to wrap my head around Javascript and jQuery. So my question is this: Is there an easy way to set rails 3.2 up to use Javascript instead?
At the moment, my jQuery is in <script></script> tags in my view (timeline/index.html.erb). I'd like to move it into a .js file. I tried changing the name of my timeline.js.coffee to just timeline.js and putting the jQuery in there, but I get Uncaught SyntaxError: Unexpected token ILLEGAL.
What should I do besides the obvious answer of "learn coffeescript"?
I was on the right track. To switch to javascript in rails 3.2 you only need to remove the .coffee extension. However, you also need to make sure you use // for comments instead of #
//# Place all the behaviors and hooks related to the matching controller here.
//# All this logic will automatically be available in application.js.
//# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Manifest Files and Directives
http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
add js files in app/assets/javascripts, then add the files in application.js
your js file
alert("here");
application.js
//= require_tree .
or
//= require your_js_file_name
then, it will be picked up from the code below in your app/views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
$ rails -v
Rails 3.2.11
how about this?
http://bit.ly/VHEnBX
git clone it
cd js_test
bundle
rails s -d
open http://localhost:3000
# stop the detached server
kill -9 `cat tmp/pids/server.pid`
you should see here alert from the browser.
it's just a simple js file. no .coffee extension or whatsoever.
http://bit.ly/UPe2mp
you can simply comment out gem 'coffee-rails' in your Gemfile