I have a problem with asset preocompilation in Rails (3.2.7).
I am including an favicon like this:
<link rel="icon" type="image/png" href="<%= image_path("favicon.png") %>" />
On development mode I set config.assets.compile = true. There everything works fine, the rendered HTML looks like this:
<link rel="icon" type="image/png" href="/assets/favicon.png" />
But on production, where I set config.assets.compile = false, I get the error
Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Home#index
...
favicon.png isn't precompiled
I already ran rake assets:precompile and i can clearly see, that the asset is available under public/assets/favicon.png.
I know, that i could set config.assets.compile = true in production, but i don´t want to do that (because of performance reasons).
Has anyone an idea, why my rails app is not able to resolve the correct path to the asset in production? Thanks!
Update:
maybe also useful to know: It happens not only for images, also for other assets.
For example <%= stylesheet_link_tag "screen", :media => "all" %> also produces the error screen.css isn't precompiled when config.assets.compile is set to false.
You must tell Rails which assets are to be precompiled. You do this in config/application.rb or config/environments/production.rb with the config.assets.precompile config key.
Rails starts with a default list of assets to precompile, including ["application.js", "application.css"], but if you want your own assets to be precompiled too, you have to add them to the list.
For example:
# config/application.rb
module MyApp
class Application < Rails::Application
# other config ...
config.assets.precompile += ["screen.css", "*.png"]
end
end
Ok, after a couple of tries I figured out, how to fix this. Nevertheless it´s a little bit strange and does not satisfy me completely. It only worked for me, when I set digest to true and provide the path to the manifest:
config.assets.compile = false
config.assets.digest = true
config.assets.manifest = Rails.root.join("public/assets")
It would be interesting to know, what´s behind this "logic".
Related
I know that there have been other questions about this, but I've read through them all and haven't figured this out yet. I'm using the PDFKit gem to allow users to generate .pdfs of content on my site. I can render a pdf by adding .pdf to the url, but CSS doesn't transfer along with it. I understand from Googling that it's because of the asset pipeline, but I haven't figured out how to make it work.
I've installed both of teh necessary gems:
gem 'pdfkit', '~> 0.6.2'
gem 'wkhtmltopdf-binary'
In my application.rb file I added 'require pdfkit' at the top and the following to the application class:
config.middleware.use PDFKit::Middleware
config.cache_classes = true
config.eager_load = true
I then ran rake middleware
I also added an initializer called pdfkit.rb with teh following code that I found, in order to fix the .pdf from endlessly loading:
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}
At this point adding .pdf to a url creates the pdf, but doesn't render the CSS at all. The README on pdfkit's github says:
"Resources aren't included in the PDF: Images, CSS, or JavaScript does not seem to be downloading correctly in the PDF. This is due to the fact that wkhtmltopdf does not know where to find those files. Make sure you are using absolute paths (start with forward slash) to your resources."
My CSS is called in the head as usual:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
How do I make an absolute path? I don't think I just replace the stylesheet_tag with an ordinary
<link rel='stylesheet' href='bootstrap_and_customization.css.scss' type="text/css">
I am working on a Rails 3 app and I need to get my js files in separate files on Heroku for debugging. On the local host when I run in development ENV it works fine.
On Heroku it serves the files separately and at the same time serves application.js with all the code in it, so the code is loaded twice which creates a lot of problems.
Things I tried:
1) Setting RACK_ENV and RAILS_ENV to 'development' in Heroku
2) Setting:
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
in both production.rb and development.rb
3) copying the entire content of development.rb to production.rb
4) using
<%= javascript_include_tag "application", debug: true %>
Nothing helps, I still get application.js with all the code and all the separate files at the same time.
You kind help will be very appreciated.
Thank you!
You can turn off rails asset pipeline by setting config.assets.enabled = false in config/application.rb
I have several microsites, each with its own stylesheet assets, within a larger Middleman project like so:
project/
source/
microsite1.com/
stylesheets/
index.haml
microsite2.com/
stylesheets/
index.haml
stylesheets/
index.haml
config.rb
Now, in production, each microsite is accessed via a domain root, e.g. http://microsite1.com/. But the above directory structure is what's required by my webhost to manage these microsites, so in development it's ideal to access these at http://localhost:4567/microsite1.com/.
However, the paths that asset helpers output aren't relative. For example, in microsite1.com/index.haml:
= stylesheet_link_tag "screen"
yields
<link href="/stylesheets/screen.css" media="screen" rel="stylesheet" type="text/css">
with :relative_assets unset, and yields
<link href="../stylesheets/screen.css" media="screen" rel="stylesheet" type="text/css">
with it set. The former output is correct in the production case; the latter is correct in neither production nor development.
Is there a way to configure Middleman so that I can test at http://localhost:4567/microsite1.com/? Alternatively, is there some way I can simulate http://microsite1.com/? (I thought to try modifying /etc/hosts, though that doesn't seem to work since I'm not pointing at an IP address)
Why do you need to touch the css_dir setting at all? You should be able to use the stylesheet_link_tag helper as follows ...
<%= stylesheet_link_tag "../microsite1.com/stylesheets/microsite1" %>
... within your templates residing in source/microsite1.com. This should give you ...
<link href="/stylesheets/../microsite1.com/stylesheets/microsite1.css" media="screen" rel="stylesheet" type="text/css" />
Here is my hacky but actually pretty functional solution:
# microsite1.com/index.haml
- if development? then $asset_base = "/microsite1.com" end
# config.rb
configure :development do
helpers do
alias_method :original_asset_path, :asset_path
def asset_path(*args)
path = original_asset_path(*args)
if not path =~ ABSOLUTE_URL_PATTERN && defined? $asset_base
path = File.join($asset_base, path)
end
path
end
end
end
tl;dr I'm hooking asset_path to guarantee that all relative assets (stylesheets, javascripts, images) are prefixed with some $asset_base path if specified. (If someone better at Ruby + Middleman than me wants to advise how I might do this without a global variable, I'm all ears.)
I'm a bit confused as it seems like the application.css is including itself twice, once when it lists the resources from the manifest and then a cache of that. So when I delete an individual file it still seems to stay alive inside the application.css file.
application.css (source)
/*
*= require twitter/bootstrap
*= require_self
*= require_tree ./common
*= require_tree ./helpers
*/
Which works as expected and outputs in dev mode all the relevant individual files
development.rb
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
output
<link href="/assets/twitter/bootstrap.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/application.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/common/announcement.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/common/button.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<Blah blah>
application.css (output)
This should be blank? Since all I have in my application.css file is the manifest and no actual css but instead i get all my concatenated code 106kb long.
IE if I remove a file in the common directory, it doesn't go away. It is no longer listed in the output but the css still appears from the application.css
I had a problem like this before. It was caused after I had precompiled the assets it was going after the applcation.css inside the public folder as well as in the apps directory. I'm not sure how to fix it so that it doesn't keep happening while in dev mode but if you delete your /public/assets directory it should fix it.
Check and see if you have a public/assets folder, if you do and it's full, it's probably why you're seeing double.
There is currently (2012-09-24) a bug in rails/sprockets causing it to not properly detect imported files.
This should be fixed in rails 3.2.9 and later but in the mean time, you can work around it as follows:
Kill the rails instance
rm -rf tmp/cache
Start the rails instance
You should now be seeing the correct css.
You might want to look at
https://stackoverflow.com/a/7854902/686460
"Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets"
That did it for me.
#Agustin's solution does it for me but here are a few things you need to do:
Delete everything in /tmp/cache/assets
Add config.serve_static_assets = false to development.rb or test.rb (credits to #Agustin)
Restart your server.
Make sure your application.js / .css is not cached in your browser. Go to http://localhost:3000/assets/application.js?body=1 and hit ctrl+f5 to force refresh (you can also try appending appending a randomizer param at the end: http://localhost:3000/assets/application.js?body=1&rnd=12343
If you get something else and ctrl+f5 still hasn't helped, you need to clear your browser's cache.
Skipping either of these steps returned a cached application.js / .css conflicting with my updates in single files.
The best way, that worked for me is to delete content of tmp/cache/* directory...
The thing that worked for us was setting 'config.assets.debug = false'
This no longer set the HTML included CSS as href="/assets/bootstrap-new.css?body=1", instead set it up as href="/assets/bootstrap-new.css", which I think was the issue.
I had the same problem. Despite clearing tmp/cache and public/assets the minified application.css was still cached and served up from somewhere and my changes to individual css files were not getting served.
This worked for me:
in application.css remove the line *= require_self
restart server
that seems to remove the cache and if you click on application.css in your browser source you will no longer see the minified version
replace the *= require_self back into the file and continue developing.
There was one last step required for me, but I got it fixed. Here's what I did:
shut down the rails server
rake assets:clean
rake tmp:clear
restart the rails server
Then, I refreshed my screen in Google Chrome, and IT STILL DID NOT WORK. So, I launched Firefox and voila, it was actually working. This means that Chrome was caching the old files within the browser. So, I then cleared out the browser cache in Chrome, and it worked!
I know this is an old question, but one fix that worked for me is that I had nginx proxying to my development environment and it had a location ~ ^/(assets)/ block in the configuration. Either comment it out, or try restarting nginx to invalidate the cache. If you're developing, you'll likely just want to comment it out entirely.
I lost way too much time troubleshooting this until I remembered that.
The assets do their job better when running the app in production environment then you'll have loading only the application.css with all the files included, and compressed, there to reduce the server request and this application.css with the compressed styles will be cached.
http://guides.rubyonrails.org/asset_pipeline.html
I have in
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/application.js" type="text/javascript"></script>
but when I go to http://localhost:3000/assets/application.css
Routing Error
No route matches [GET] "/assets/application.css"
P.S. Rails 3.1.0.rc4, ruby 1.8.7
Seems Sprockets / Rails 3.1 were acting up for me w/ ruby 1.9.2-p180 ... updating to Ruby-1.9.2-p290 seemed to stop the issue.
Maybe not related to your issue... but useful for anyone else having that issue using those versions of Rails & Ruby.
I found that I had something similar going on after updating to Rails 3.1 this evening. I was working on a project that didn't use ActiveRecord, so I had a modified my application.rb to exclude it. The line that usually reads require 'rails/all' to only include the parts I needed, like this:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
This list has changed in Rails 3.1 to include Sprockets, the core component to making the asset pipeline work. I got the asset pipeline serving the serving content as expected by adding this line to the bottom of the list:
require "sprockets/railtie"
After restarting, /assets/application.js and other assets began working as expected.
Note: if you have a custom setup like this, be sure to open the railties gem and look at the contents of lib/rails/all.rb which may have changed (as in this case).
Your scripts and styles will be loaded from the public folder. Drop the assets folder under public and you should be good to go.
In the application layout file, if you have
<%= stylesheet_link_tag "/assets/application" %>
which gives
No route matches [GET] "/assets/application.css"
TRY changing it to
<%= stylesheet_link_tag "application" %>
I had to add the following line in application.rb:
config.assets.enabled = true
in bottom of class Application < Rails::Application