Rails assets pipeline debug in test - ruby-on-rails

I have the following in config/environments/test.rb:
config.assets.debug = true
config.assets.enabled = true
I get all my js files concatenated into application.js
However, when I do the same in development, I get javascript file included by itself.
Is there a way to make test (I'm using cucumber) behave like development?
The reason I'm asking is because I would like to be able to modify javascript and run my cucumber tests right away instead of having to do rake assets:precompile RAILS_ENV=test every time.
Thanks!

I turns out I had a stupid hard coded line:
I had the following code and I suggest you never pass the :debug option to javascript_include_tag but instead, configure everything in your development.rb and test.rb.
<!-- watch out when passing the :debug option to javascript_include_tag -->
<%= javascript_include_tag "application", :debug => (Rails.env == "development") %>

in your application.rb file you may need to add test to the following line:
Bundler.require(*Rails.groups(:assets => %w(development test)))

Related

Rails not serving assets in development mode

Well it's the first time I set up a Rails app from the beginning and I just created my first view.
When I run the server I get a 404 Not Found error when trying to load the application.js file.
Layout
<html>
<head>
<%= yield :head %>
</head>
<body>
<h1>If you're not and Admin you shouldn't be here!</h1>
<%= yield %>
<%= javascript_include_tag 'application' %>
<% yield :script %>
<script>
$(function(){
alert('Hey')
})
</script>
</body>
</html>
application.js
//= require jquery
environment/development.rb
...
config.assets.debug = false
config.assets.compile = false
config.assets.digest = true
...
And when I run on terminal rake assets:precompile I get this error:
rake aborted!
Don't know how to build task 'assets:precompile'
What is causing this?
To resolve this, add the following to config/application.rb:
require 'sprockets/railtie'
For more information see here.
Try to set config.assets.compile = true and try again. It should work after that.
Cheers!
After running rake assets:precompile --tasks the precompile task was not listed.
I found out that a require on config/application.rb was missing:
require 'rails/all'
After adding this the app started precompiling and serving assets (actually there were minor errors with the gems I tried to install, I just had to remove the configuration files and then everything worked fine)

Rails - Loading assets through the Asset Pipeline

I am trying to set up asset pipeline on a older rails app that i have so that i can start using CoffeeScript. i am using ruby 1.9.3-p327 and Rails 3.2.13. I used to stash all my JS, CSS and my images in the public/ folder. This is what i have done so far ->
I moved them all to app/assets, i added the manifest files for both JS and CSS call //= require_tree ..
Added the following gems
group :assets do
gem 'coffee-rails'
gem 'uglifier'
gem 'sass-rails'
gem 'therubyracer'
end
Removed all my javascript_include_tags except for = javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'
Added the following to my config/application.rb file
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
Within the config/environments/development.rb file set
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
I added to the config/environments/production.rb
# Compress JavaScripts and CSS
config.assets.compress = true
# Choose the compressors to use
# config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :yui
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
I re-read the section on the Asset Pipeline just to see if i messed up anything. But so far none of the assets are pulling through from app/assets the only one that is working is the jquery.min.js that i have coming in through include_tag i have tried removing it and trying again no dice.
I have tried bundle exec rake assets:clean, and bundle exec rake assets:precompile, both of which run without issues. and have bundled since adding the gems for the assets, and restarted pow each time.
I am not sure if i am going through this all wrong or i have missed a step? any one gone through this before and tips or clues would be much appreciated.
You said you removed all the javascript_include_tag statements. Assuming you named the manifests app/assets/javascripts/application.js and app/assets/stylesheets/application.css, you'll need to include those in your layout:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
Otherwise, Rails won't know to load your manifest files
Also, you shouldn't be using rake assets:precompile in development. If you do, then anytime you change a JS/CSS file, you'll have to recompile the assets before your changes show up.

rails how do i configure assets:precompile for creating files inside public/javascript or /public/stylesheets

i have a rails application that i want to deploy on my production server,
the thing is that when i run rake assets:precompile, it generates css and js files under /public/assets , for example "/public/assets/application.css" and "/public/assets/application.js"
but when i add this tags on my erb
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
and i run the site, they both refer to
"mysyte/javascript/application.js"
and
"mysite/stylesheets/application.css"
so, the question is, how do i configure the app so that when i precompile my assets they are place inside "/public/javascript", "/public/stylesheets", and "/public/images"
thanks in advance.
EDIT:
i guess the real problem is that on production the following tag
<%= stylesheet_link_tag "application", :media => "all" %>
is refering to "www.mydomain.com/javascript/application.js"
how do i change it to refer to
"www.mydomain.com/assets/application.js"
i've solved this,
the thing is that i'm using mongoid on my app, and had to remove active_record
so in my application.rb
i removed require "rails/all"
and in change i added
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
i was missing
require "sprockets/railtie"
now my app is searching for assets in the right assets folder.
thanks richlewis and deefour for your time.

rails 3.2 javascript_include_tag with 'cache => true' option

I'm using rails 3.2.5. I used 'cache => true' option to combine javascript files. I wrote following code.
<%= javascript_include_tag "a.js", "b.js", :cache => true %>
I get the following error when I try to load the page.
No such file or directory - Asset file not found at '/home/MyProjectPath/public/javascripts/home/MyProjectPath/app/assets/javascripts/a.js'
a.js and b.js are located in 'app/assets/javascripts/'.
If I use this code,
<%= javascript_include_tag "a.js", "b.js" %>
It works fine. But they remain separate javascript files.
How can I merge javascript files using :cache option?
I think what you're looking for is :compress, not :cache . The error message you get is because the ":cache" option was removed, see the note in:
http://edgeguides.rubyonrails.org/asset_pipeline.html#in-production
Make your asset enable false in in application.rb file
config.assets.enabled = false

rails 3.1.0 ActionView::Template::Error (application.css isn't precompiled)

I made a basic rails app with a simple pages controller with an index function and when I load the page I get:
ActionView::Template::Error (application.css isn't precompiled):
2: <html>
3: <head>
4: <title>Demo</title>
5: <%= stylesheet_link_tag "application" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__43625033_88530400'
Gemfile
source 'http://rubygems.org'
gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
By default Rails assumes that you have your files precompiled in the production environment, if you want use live compiling (compile your assets during runtime) in production you must set the config.assets.compile to true.
# config/environments/production.rb
...
config.assets.compile = true
...
You can use this option to fallback to Sprockets when you are using precompiled assets but there are any missing precompiled files.
If config.assets.compile option is set to false and there are missing precompiled files you will get an "AssetNoPrecompiledError" indicating the name of the missing file.
You will get better performance in production if you set config.assets.compile to false in production.rb and precompile your assets. You can precompile with this rake task:
bundle exec rake assets:precompile
If you are using Capistrano, version 2.8.0 has a recipe to handle this at deploy time. For more info, see the "In Production" section of the Asset Pipeline Guide:
http://guides.rubyonrails.org/asset_pipeline.html
OK - I had the same problem. I didn't want to use "config.assets.compile = true" - I had to add all of my .css files to the list in config/environments/production.rb:
config.assets.precompile += %w( carts.css )
Then I had to create (and later delete) tmp/restart.txt
I consistently used the stylesheet_link_tag helper, so I found all the extra css files I needed to add with:
find . \( -type f -o -type l \) -exec grep stylesheet_link_tag {} /dev/null \;
A quick fix for capistrano user is to put this line to Capfile
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
For all those who are reading this but do not have problem with application.css and instead with their custom CSS classes e.g. admin.css, base.css etc.
Solution is to use as mentioned
bundle exec rake assets:precompile
And in stylesheets references just reference application.css
<%= stylesheet_link_tag "application", :media => "all" %>
Since assets pipeline will precompile all of your stylesheets in application.css. This also happens in development so using any other references is wrong when using assets pipeline.
I was having the exact same error in my development environment. In the end all I needed to do in order to fix it was to add:
config.assets.manifest = Rails.root.join("public/assets")
to my config/environments/development.rb file and it fixed it. My final config in development related to assets looks like:
config.assets.compress = false
config.assets.precompile += %w[bootstrap-alerts.js] #Lots of other space separated files
config.assets.compile = false
config.assets.digest = true
config.assets.manifest = Rails.root.join("public/assets")
config.assets.debug = true
I also had this issue, where trying to run in production without precompiling it would still throw not-precompiled errors. I had to change which line was commented application.rb:
# If you precompile assets before deploying to production, use this line
# Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
Bundler.require(:default, :assets, Rails.env)
Here's the quick fix:
If you're using capistrano do this add this to your deploy.rb:
after 'deploy:update_code' do
run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
end
cap deploy
I ran into this error message today and wanted to post the resolution to my particular my case. It turns out that my problem was that one of my css files was missing a closing brace and this was causing the file to not be compiled. It may be harder to notice this if you have an automated process that sets everything up (including the asset precompile) for your production environment.
After all else failed...
My solution was to change the layout file from
= stylesheet_link_tag "reset-min", 'application'
to
= stylesheet_link_tag 'application'
And it worked! (You can put the reset file inside the manifest.)
Just another way to fix this up on Heroku: Make sure your Rakefile is committed and pushed.
On heroku server (readonly filesystem),
If you want runtime compilation of css (its not recommended but you can do it), make sure you have done settings like below -
# inside config/application.rb
config.assets.enabled = true
config.assets.prefix = Rails.root.join('tmp/assets').to_s
# If you are using sass then keep gem outside of asset group
gem 'sass-rails', '3.1.4'
# inside config/environments/production.rb
config.assets.compile = true
if you think you followed everything good but still unlucky, just make sure you/capistrano run touch tmp/restart.txt or equivalent at the end. I was in the unlucky list but now :)
You probably have a syntax error in the css you are using.
Run this command
$ bundle exec rake assets:precompile RAILS_ENV=development --trace
It will give the exception, fixed that and you are all done.
Thanks

Resources