So my application.js file is including jquery accordingly, and everything I've put directly into it.
However, one of my controllers - lets call it Books - has its own books.js file
I want the books.js file only to be present when viewing pages within the books controller.
It doesn't seem to be including it at all though - any ideas?
First you should look at http://railscasts.com/episodes/279-understanding-the-asset-pipeline
Second in app/assets/javascripts/application.js
does it contain something like
//= require_directory .
or
//= require_tree .
try:
<%= javascript_include_tag params[:controller] %>
"everything I've put directly into it." ...does that mean you put your own code into application.js? If so, you might want to read a tutorial on the asset pipeline.
But anyways, sounds like //= require_tree . is missing.
Related
I am trying to install a plugin called Chaffel.js, I have added it to my javascript file
+app
|+assets
||+javacripts
|||chaffle.min.js
|||
I have required it in application.js
//= require chaffle.min.js
//= require rails-ujs
//= require turbolinks
//= require_tree .
And I have also added this to config/initializers/assets.rb
Rails.application.config.assets.precompile += %w(chaffle.min.js)
And I have included it in my view
<%= javascript_include_tag 'chaffel.min' %>
And have added all the html / javascript it showed for the example but when I load up the view it gives me this error
Sprockets::Rails::Helper::AssetNotFound in Home#index
The asset "chaffel.min.js" is not present in the asset pipeline.
I don't know if it is a problem with my asset pipeline or if the plugin just doesn't work anymore (As the cdn src on its page doesn't seem to work either) Would love some help with this or recommendations for a different plugin/way to achieve the same effect (a text shuffle animation).
I was able to get it working in a normal html file by just directly including the file via
<script src="chaffle.min.js" charset="utf-8"></script>
So it is definitely a problem with my code / the asset pipeline and not the plugin.
First of all, if you have //= require_tree . in your application.js you don't need to change any other files. //= require_tree . will include all files in the javascript directory for you:
http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
But the problem you have faced, I guess is a just a typo: chaffle / chaffel. But again, just clear all mentions of it and leave only require_tree – that should be enough.
I'm trying to use Bootstrap Combobox for rails.
The instructions say that:
*"You will need two files included in your HTML in order for this to
work:
js/bootstrap-combobox.js
css/bootstrap-combobox.css*
I realise that they need to go in the application js and application css respectively, but I think that I'm getting the syntax wrong because I'm getting an error.
This is how I'm adding to application.css.scss:
* require css/bootstrap-combobox.css
This is how I'm adding to application.js:
//= require bootstrap-combobox
What error are you getting?
Without knowing the exact error, here's what I suggest:
After you make sure you have a file in your assets/stylesheets named bootstrap-combobox.css, you want your require statement to look like this:
*= require bootstrap-combobox
Your require statement looks fine for the JS assuming you have the file in the assets/javascripts folder.
You can also use //= require_tree . and *= require_tree . to require all files in your javascripts and/or stylesheets folders respectively instead as well.
Check out this section of the Rails asset pipeline for more info.
I have a view with the following namespace
view/kids/registration/new.html.haml
Now I need to create a js file to this view, so I created a file in the following path:
assets/javascripts/kids/registration/new.js
Then I add the following lines to the application.js:
//= require ./kids/registrations/new
But it does not work. What I'm doing wrong. I'm checking the DOM (localhost:3000/kids/sign_up) but I never find line with this javascript file.
Thanks in advance for your help.
Adding the route
new_kid_registration GET /kids/sign_up(.:format) frontend/registrations#new
What I'm doing wrong.
Thanks.
Try //= require kids/registrations/new or //= require kids/registrations/new.js
I had faced similar problems while adding stylesheets to assets (Link to my question)
Here are few things you might wanna try:
As suggested by BOB add the require statements to application.js
I remember adding the list of files to application.rb as:
config.assets.precompile += ['jquery.colorbox.js','colorbox.css', 'reportng.css', 'reportng.js']
Your JS file can be placed anywhere in lib/assets/javascripts,
assets//assets/javascripts & vendor/assets/javascripts
Try using the
<%= javascript_include_tag "your_js_file_name" %> in your view file
For site wide specific JS code (i.e. for the header, which appears on all pages). Where should this be placed? In:
app/assets/javascripts/application.js
Is that right?
For pages#home. Which root_url also points to (root :to => 'pages#home'). Where should my JS file be placed in the pipeline?
app/assets/javascripts/pages/home.js
And regards to my application.js. Is this right? It currently looks like:
//= require jquery
//= require jquery_ujs
//= require_directory .
Or should I just embed page specific JS in the view?
It's not a good idea to put js code in application.js. You can put that code in any other file on the assets/javascripts folder and it will be included automatically by the require_directory or require_tree command. Your application.js file is perfectly fine as it is right now, but you might want to use require_tree instead of require_directory for recursive inclusion.
For example, the javascripts files in app/assets/javascripts/pages will be included by require_tree but not by require_directory.
Recommended reading: Asset pipeline guide
Prior to rails 3.1, javascript code that was common to the application belonged in application.js by default, and was loaded by javascript_include_tag :defaults
With the asset pipeline in rails 3.1, the application.js file becomes a manifest file, and it appears that code I put in it is not included in the result. Where is this javascript code supposed to be moved to now? Obviously, I could create any other name and make sure that it is included by the manifest, but is there a default location already expected by idiom?
I encounter the same problem in rails 3.1 rc6. I use javascript_include_tag :application instead
Look closer:
the code in application.js is rendered, it's just at the end of the resulting js file.
Example, try:
//= require jquery
//= require jquery_ujs
//= require_tree .
alert('foo');
the alert dialog will appear in all pages.