I have added these two lines to application.rb:
config.assets.initialize_on_precompile = false
config.assets.compile = true
However I still get errors when I push to Heroku:
2012-02-05T09:48:34+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2012-02-05T09:48:34+00:00 app[web.1]:
2012-02-05T09:48:34+00:00 app[web.1]: ActionView::Template::Error (bootstrap.css isn't precompiled):
Any suggestions?
By the looks of it you have a bootstrap.css file that is not included properly in your manifest file within app/assets/stylesheets and that you're probably also calling directly from a stylesheet_tag.
There are a couple of approaches to this:
You could add a line to your environment config file which will ensure the css file you're calling is precompiled:
config.assets.precompile += %w( bootstrap.css )
…for example.
This is the one i would probably do; include the bootstrap.css file in a manifest file inside `app/assets/stylesheets' as mentioned above. Your manifest file will look something like this (not sure if the formatting of this will appear correctly on here, so i have also created a Gist: https://gist.github.com/1753229):
/*
*= require bootstrap
*/
/* rest of file omitted */
You might need to require more files than that depending on what your css setup is.
Try to use rake assets:precompile before committing your code and pushing it to heroku.
read this, it's a tutorial on how to get rails 3.2.1 (and ruby 1.9.3) running on heroku. You can obviously skip the bits you don't need but it should explain why these problems are happening and how to fix them.
Also, read this article by david rice, author of the useful asset_sync gem. It will help you sort this out.
Related
I have Rails 6, my preview class located in
mailer/previews/devise_mailer_preview.rb:
class DeviseMailerPreview < ActionMailer::Preview
...
end
And when I run application locally, everything is going fine, I can see my email previews on http://localhost:3000/rails/mailers/devise_mailer/confirmation_instructions address. But now Im trying to deploy application on server, and found that when I run bundle exec rails c production, I got the error:
/home/deploy/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/loader/callbacks.rb:17:in
`on_file_autoloaded': expected file
/home/deploy/project/releases/20200627024908/app/mailer/previews/devise_mailer_preview.rb
to define constant Previews::DeviseMailerPreview, but didn't
(Zeitwerk::NameError)
After that I've checked locally RAILS_ENV=production rails c, and got same.
If I will rename DeviseMailerPreview class to Previews::DeviseMailerPreview, it will be broken and I cannot see emails on development, because Rails 6, accordingly to docs, expect exactly that name.
More of that, I've found in this article, that zeitwerk can be configured with autoload_paths param to avoid ruby's NameError. I found that I have it my config/application.rb:
config.load_defaults 6.0
Anyway I tried to add same row in my config/environments/production.rb file, but it didn't help.
What am I doing wrong and how can I fix it? Thanks in advance!
Add preview_path to the autoload_paths and zeitwerk will expect DeviseMailerPreview constant to be defined.
# development.rb
config.action_mailer.preview_path = Rails.root.join("app/mailers/previews")
# application.rb
config.autoload_paths << Rails.root.join("app/mailers/previews")
Your Mailer preview file is located in mailer/previews/devise_mailer_preview.rb, so I'm assuming it's full path is app/mailer/previews/devise_mailer_preview.rb
The docs says
In the above example, the preview class for UserMailer should be named UserMailerPreview and located in test/mailers/previews/user_mailer_preview.rb
So put your devise_mailer_preview.rb file to test/mailers/previews/devise_mailer_preview.rb
or in your config/application.rb add this line and restart:
config.action_mailer.preview_path = "#{Rails.root}/app/mailers/previews"
Actually the answer was in the docs itself.
In Rails 6, previews are added to the autoload paths only if options.show_previews is true, which is not by default in the production environment. See the source code here.
The reason for this is that previews are supposed to be an aid for development, and they are generally not something you want to be able to look at in production.
However, you can set that flag to true in production if you want.
There's another derivative: By storing the previews under app/mailers, Rails is going to eager load them because app/mailers is in the autoload paths. If app/mailers/previews is not in the autoload paths, eager loading will fail due to the namespace mismatch. Either you have them enabled in all environments, or else is better to have them in a separate location, like the default.
This is incredibly frustrating.
I have added a few CSS files to my vendor assets, and I cannot seem to get them to get precompiled, or at least FOUND by my production server.
The files are found in something like this:
/vendor/assets/stylesheets/cssfw/style.css
/vendor/assets/stylesheets/cssfw/app.css
/vendor/assets/stylesheets/cssfw/plugins.css
/vendor/assets/stylesheets/cssfw/custom.css
And they're loaded like this on my application.css
*= require cssfw/style.css
And inside style.css
#import url(app.css);
#import url(plugins.css);
#import url(custom.css);
This works fine in my development environment (obviously), but all of those 4 CSS files cannot be found anywhere in the assets in production.
I'm obviously missing to include something, but what?
If I use something like this in production.rb:
config.assets.precompile += %w( *.css )
I get a barrage of errors, cause then it goes through every single stylesheet I have in the /vendor/ directory. if I try to be specific (such as specifying style.css), the same initial errors persist, and style is nowhere to be found.
How are they supposed to be included exactly? Preferably, I wouldn't want to include ALL the vendor assets files, cause there's only a handful that I'm actually using, but the folder contains around 20 or so that I haven't yet configured.
I'm using Rails 4.1.6 with Ruby 2.1.0, on Apache Phusion Passenger.
The trick was in changing #import to *= require, cause #import isn't following the assets pipeline. I'm still unclear of why that happens, but I changed this in style.css, which after all was actually being loaded all this time.
In my application.css:
*= require cssfw/style # /vendor/cssfw/style.css
And in the first part of style.css:
/*
*= require cssfw/app # /vendor/cssfw/app.css
*= require cssfw/plugins # /vendor/cssfw/plugins.css
*= require cssfw/custom # /vendor/cssfw/custom.css
*= require cssfw/plugins/animate # /vendor/cssfw/plugins/animate.css
*= require cssfw/plugins/box-shadows # /vendor/cssfw/plugins/box-shadows.css
*/
This would make Rails to look for them in the proper places in the vendor pipeline, although I'm being forced to practically include the entire path, minus cssfw. If I leave it out, it works fine in development, but production cannot find them.
I didn't move anything else to my application.rb, or production.rb, in case you're wondering. I think this may have a more elegant solution, but this works for now.
you should set in production.rb:
config.assets.compile = true
and in command line use:
rake assets:precompile
before launching it your server.
I'm trying to put my first app into production and ran into the following when trying to precompile:
rake aborted!
Undefined variable: "$yellowCorp"
(in app/assets/stylesheets/_dialog.scss)
$yellowCorp is declared in
app/assets/stylesheets/_variables.css.scss
I've done quite a bit of reading and there seems to be quite a few people that have struggled with this. I've tried many solutions that have worked from them, but none work for me. In config/environments/production.rb I've tried
config.assets.precompile += %w(*.css.scss *.scss)
and
config.assets.precompile << [ "*.scss", "*.css", "*.js" ]
and
config.assets.precompile += [ "_variables.css.scss", "_dialog.scss" ]
...among others, but nothing changes, but nothing have gotten me past this point. Any help/advice would be greatly appreciated.
Thanks!
I imagine this problem stems from the order in which the files are compiled. It is compiling the dialog file before the variables.
What does your application.css.scss file look like?
Instead of requiring tree, you may have to specify the order in which to compile them:
#import "variables.css.scss"
#import "dialog.scss"
I recently migrated from Jammit to the Rails Asset Pipeline. Other than a few teething issues, everything has been working well.
However, I recently started getting some script errors in production, and realised that it's near on impossible for me to debug them. I had previously configured Jammit to retain linebreaks, but otherwise remove all white space in the javascript files. This was to ensure that should I see a runtime error, I would be able to locate the offending line and hopefully figure out what the problem is. With the Rails Asset Pipeline, and the default :uglifier compressor, it appears all whitespace is removed including line breaks, and as such my script errors do not tell me where in the code the problem was.
Does anyone know anyway to configure the Rails Asset Pipeline to retain line breaks so that code can be debugged?
Matt
Set in you production.rb:
config.assets.compress = false
and running rake assets:precompile won't uglify your assets.
UPD:
So-called compression means (among other stuff): remove line breaks and comments.
But if you want to obfuscate your variables and save some readability then use:
# in production.rb
config.assets.compress = true
config.assets.js_compressor = Uglifier.new(:beautify => true) if defined? Uglifier
Here see for more options: https://github.com/lautis/uglifier.
Bit of a newbie question. I can't seem to get my application to pick up images
that I have in a local folder ( public/stylesheets/images/XYZ/*.png). As a result
my main page is rendered without some essential graphics.
In my HAML file, I have tags defined as follows:
%img{:src => '/images/XYZ/scissor.png'}
This leads to calls like:
Started GET "/images/dookum.in/scissor.png' for 127.0.0.1 ....
and error messages like:
ActionController::RoutingError (No route matches "/images/dookum.in/scissor.png')
I don't know why this is happening. Do I need to define RAILS_ROOT? Or change routes.rb? If yes, then how?
Thanks for your help
Abhinav
You may try to add following setting in your environment files
config.serve_static_assets = true
Either you write
= image_tag('scissor.png')
and this will look for the file /public/images/scissor.png, or you should specify stylesheets/images/XYZ/...
Everything under /public is served in development mode, if you want you rails process to serve everything under /public in production-mode, you have to set the config.serve_static_assets to true.