Assets not being required by Rails - ruby-on-rails

For some reason, the js files under app/asset/javascripts folder are no longer being requested by the rails application.
I have enabled assets in application.rb
I have these lines in application.js:
//= require_self
//= require_tree .
with proper use of the blank lines.
And I have included this line in my application.html.erb file:
<%= javascript_include_tag "application" %>
<%= csrf_meta_tag %>
I have application.js, jquery.js, jquery-ui.js, prototype.js and some other js files in the javascripts folder.
But when I access those js files in Firefox firebug, the content are different from that in the folder. I keep getting the message: Served asset /xxx.js - 304 Not Modified (0ms).
It is so frustrating and I really need some help.
Thank you in advance!
I am using
Rails version: 3.2.7
Ruby version: ruby 1.9.3p194
gem version: 1.8.24
Thin server
UPDATE:
I have used rake assets:clean and this never happened again!

Probably this is beacuse to you have this precompiled files in public dir
try adding this to development.rb
config.serve_static_assets = false

Related

Zurb Foundation 5, modernizr not found

I get this when using Foundation 5.0.2.0 in production mode.
On Rails, Unicorn, NginX & Ubuntu.
"NetworkError: 404 Not Found - http://mydomain.com/javascripts/vendor/modernizr.js"
Update 3/13/14
I don't have this problem on heroku with foundation-rails-5.1.1.0. Adding javascript_include_tag "vendor/modernizr" in the head works. I could remove the modernizr I copied to the vendor directory and remove the extra line in app.js
I had the same problem on heroku, the app would crash because it couldn't find modernizr.js. Here's how I fixed it:
Copy modernizr.js from foundation (wherever you installed it with bundler) into vendor/assets/javascripts/.
Add //= require modernizr below //= require_tree . on application.js.
Remove javascript_include_tag "vendor/moderizr" from application.html
I got the same error as you and I did this to fix it:
In config/environments/production.rb, set this:
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
Try updating the gem you're using for Foundation. This commit from five days ago may help you since it claims to add a missing modernizr.js file.
There is an issue with the version of the foundation-rails gem you are using. In fact even the latest version currently available (5.0.3.1) has this problem. There is a pull request which claims to fix this: https://github.com/zurb/foundation-rails/pull/71 but another option for now is to update your gem to 5.0.3.1 and add this line to your production.rb file:
config.assets.precompile += %w( vendor/modernizr.js )
Please follow the manual instructions from zurb here:
http://foundation.zurb.com/docs/applications.html
Which specifiy that the modernizr script be included inside the head tag. I ran into this issue and then fixed it using the following code in layouts/application.rb:
%head
%title= title
= stylesheet_link_tag "application"
= javascript_include_tag "vendor/modernizr"
= csrf_meta_tags
= favicon_link_tag
= yield(:head)
I am using foundation-rails (5.2.2.0) and I solved it by just adding:
//= require modernizr
below
//= require_tree .
in app/assets/javascripts/application.js

Rails 3.2.8 Application.js and Application.css are not working as expcted

When i try to include
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
The content of the files of the application.css is:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require social_stream
*= require_tree .
*/
And the content of application.js file is
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require social_stream-base
//= require social_stream-documents
//= require social_stream-events
//= require social_stream-linkser
//= require social_stream-presence
//= require social_stream-oauth2_server
//= require_tree .
my rails app is not including the css and js files those are required in the above snippet.
but when i update the include tags to the specific file those are included fine.
i.e.
<%= stylesheet_link_tag "social_stream" %>
<%= javascript_include_tag "jquery" %>
What i think there is issue of sprockets its not working as expected please advice.
I will be really thankful to you.
It should be ruby version issue. You would be using ruby 2.0.0, try to downgrade to 1.9.2 or 1.9.3. Hope it will work.
Nazar Hussain's answer helped me. It depends on ruby version too. If you are using v.2 then try to downgrade to v.1.9. It is very easy if you are using rvm:
To install v.1.9.3 use:
$ rvm install 1.9.3
To use v.1.9.3 as default:
$ rvm --default use 1.9.3
Then
restart your terminal
install bundler gem
run bundle install
run rake assets:clean RAILS_ENV=development
start your server rails s
That's all.
I was also having this issue but with newer versions of Rails and Ruby.
Examining the log, I was being served javascript.js from Rails cache as the server didn't see a change in the file. I went and moved the require lines around (just one) to tell Rails that there's a change in the file and re-compile/use. Wellm that did it for me!
Hope it helps somebody.
Another important finding is to upgrade your sprockets gem in your Gemfile.
I had version 2.2.1 and had issues, after upgrading to 2.2.2, it worked
gem 'sprockets', '2.2.2'
Are you using sass or less or something similar?
I just had this problem and I found it was because the application.css file contained scss code, which it cannot handle by default.
Simply renaming the file to application.css.scss resolved this issue under Rails 4.2.1.

rails css manifest file skips directives

rails newbie here. Can't get development machine to process *= require ... in application.css manifest file. In development.rb I have
config.assets.compress = false
config.assets.compile = true
config.assets.debug = true
In app/assets/stylesheets there is application.css with
*= require_self
*= require_tree .
and the app.css.scss
If I request http://localhost:3000/assets/app.css The .scss is processed fine,
But the output application.css to the browser still has
*= require_self
*= require_tree .
in it, instead of the .scss file that is in the same directory. What do I need to enable the asset pipeline to process the require directives? bundle install is working fine. I have also tried with the precompile rake task with same results. It seems the directives are being ignored.
My include tag is <%= stylesheet_link_tag "application" %>
This is a Refinery site that I am trying to setup a development environment for.
Thanks in advance
Finally found out what was up. Had the wrong version of Ruby installed. Since this was an existing Rails application, the Gemfile specified gem versions which were I guess incompatible with ruby 2.0.0, which was what installed by default on my Ubuntu machine.
Fix:
rvm install 1.9.3
rvm use 1.9.3 --default

How can I correctly deploy static and compiled assets in rails with nginx and capistrano?

In my project, I have tried to bundle most of my js libraries into gems so they can be updated and pulled in whenever necessary, however, I've recently setup our production server and I'm having issues with asset compilation and static assets in /public/assets.
Specifically, when I don't use the asset pipeline, I have no issues with datatables or any other JS libraries or css that I am trying to use. I am precompiling my assets in production and using a typical nginx setup (based on railscast #335) to serve them.
Static assets are the following -
public/assets
javascript/jquery.formatCurrency-1.4.0.min.js
stylesheets/datepicker.css
twitter/bootstrap/bootstrap-datepicker.js
is public/assets the correct place for these?
When I deploy with capistrano, these assets get a 404 when loading the page and are not actually copied to the /public/assets directory on the server.
When loading other pages I am calling the datatables plugin as follows
$('#inventory_item_list_datatable').dataTable
sDom: "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
sPaginationType: "bootstrap",
iDisplayLength: 100
Datatables is compiled into my application.js and doesn't throw an error, but the table never actually renders. I'm wondering if this is related to the order that everything is compiled?
app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require jquery_nested_form
//= require twitter/bootstrap
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.bootstrap
//= require_tree .
app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application", :media => "all" %>
<link href="/assets/stylesheets/datepicker.css" media="all" rel="stylesheet" type="text/css" />
<%= javascript_include_tag "application" %>
<script src="/assets/twitter/bootstrap/bootstrap-datepicker.js" type="text/javascript"></script>
---edit datatables issue
when using jquery-datatables-rails gem it must be OUTSIDE of your assets group in gemfile.
By default , asset-pipeline comes with 3 locations for placing the assets : app/assets, lib/assets and vendor/assets . You have to place (in case you'd like to rely on the pipeline) your assets there , not in the public/ . In the production env all the assets are compressed and transformed into one file (respectively for .js and .css ) : application.* .
EDIT : In this case the assets directory was structured differently . After the chat with #theIV and #Kosmonaut , we have found the solution : moving the assets to the vendor/assets directory with structure :
For Javascript assets:
vendor/assets/javascripts/specific_javasripts
and for CSS:
vendor/assets/stylesheets/specific_stylesheets
By default , the vendor/assets/ directory does not contain javascripts nor stylesheets directories , they should be created .

asset:precompile for .js files? rails 3.1

Question:
How do you get the asset pipeline to process all your .js files? (I want them served individually, not bundled into application.js)
I'm getting a ton of 404's for the javascript files that my pages are trying to reference:
GET http://<myStagingServer>.heroku.com/assets/<javascriptFilename1_MD5fingerprint> 404 (Not Found)
GET http://<myStagingServer>.heroku.com/assets/<SubDir>/<javascriptFilename2_MD5fingerprint> 404 (Not Found)
I tried adding this to config/application.rb:
config.assets.precompile << '*.js'
But that didn't do anything as far as I can tell.
Background:
I'm upgrading from Rails 3.0 to 3.1 and enabling the asset pipeline.
Highlights so far:
Switching to Heroku's Cedar stack from Bamboo: heroku create --stack cedar.
Switching to "thin" as the production server, which fixed various issues: gem 'thin'.
Moving my assets from public/assets to app/assets, updating references in code to use stylesheet_link_tag and javascript_include_tag. (Plus whatever I did for images -- they work.)
Removing x_sendfile_header config options because Heroku doesn't support it.
Relevant files:
//
// application.js
//
//= require_self
//
OMG: I found the problem:
javascripts and stylesheets with periods in their names require explicit extensions
For example:
# WORKS
javascript_include_tag "application"
stylesheet_link_tag "application"
# BROKEN
javascript_include_tag "jueryui.custom"
stylesheet_link_tag "jueryui.custom"
# WORKS
javascript_include_tag "jueryui.custom.js"
stylesheet_link_tag "jueryui.custom.css"
I guess I can see why this is, but I think that it isn't very well documented on any of the asset pipeline tutorials. Is it common knowledge that you shouldn't have periods in your asset filenames?
I think you need the following in both application.js and application.css :
//= require_tree .
This loads all the files in the assets directory for CSS and JS.
Also for upgrading to 3.1 and info on the asset pipeline:
http://railscasts.com/episodes?utf8=✓&search=Asset+pipeline
Also: Using Rails 3.1 assets pipeline to conditionally use certain css

Resources