I want to be able to use an environment variable to swap out a site-specific stylesheet with sass variables to define colors for an instance of a Rails app. I can't seem to figure out how to use ruby code inside a sprockets directive to define the dependency.
What seems like it should work:
// stylesheets/application.css
/*
*= require "#{ENV['SITE_STYLESHEET']}"
*= require core
*= require profile
*/
Where ENV['SITE_STYLESHEET'] = my_stylesheet and stylesheets/my_stylesheet.scss exists.`
The error I receive is: couldn't find file '#{ENV['SITE_STYLESHEET']}'
Is there any way to use ruby inside a directive?
This might not be the most eloquent way to do it, but I think you could do something like this.
Create initializers/assets.rb
In this file
Rails.application.config.assets.precompile += %w( ENV['SITE_STYLESHEET'] )
Related
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 am trying to remove a line in one file on Rails. I found a method gsub_file, but it was an undefined method in Rails 4. It's like reverse method of insert_into_file of thor.
e.g.) app/assets/stylesheets/application.css
Before
*= require_tree . <- Needs to be removed!
*= require_self
After
*= require_self
This should be performed in Rails Application Template.
If you are implementing an application template (for use with the rails new myapp -m option), try using the Thor gsub_file, like this:
gsub_file 'app/assets/stylesheets/application.css', /*= require_tree .\n/, ""
Take a look at the rails_apps_composer gem if you'd like to see lots of file manipulation using Thor.
I have a lib/redirect_follower.rb file
Where I use the file, I include it with require 'RedirectFollower'
But rails is playing hard ball with this error:
no such file to load -- RedirectFollower
Any clues? Been banging my head over this for hours. Have tried auto loading all libs using application.rb but that didn't work either.
require is for including a file, not a class.
You need to require "redirect_follower", ie, the actual filename, not the class name. You may also need to add lib to your include path, or require "lib/redirect_follower".
In config/application.rb: add this:
config.autoload_paths << "#{config.root}/lib"
With this setting, your modules (i.e. files under lib/) will be automatically required so you don't have to require them anywhere (actually, you should never require them because that would have an negative effect on un/loading files by Rails).
I am working on a Rails 3.1 app. I have created an application.css.scss.erb file. The .erb is in the end because I want to load a variable from the config file as the color variable in the css:
$highlight1: #<%= COLOR.highlight1 %>;
$highlight2: #<%= COLOR.highlight2 %>;
Everything works fine, but the problem I am having is that whenever I change a value inside COLOR.highlight1, it doesn't reflect the change until I go in to my css file and change something (i usually add some spaces and save it). Thats when I see the change. Clearly rails is looking to see if the file was changed in order to update the change.
Is there any way that at least during development, this can be turned off and I can see the changes without having to also modify the css file?
Any critique/opinions on my technique are also welcome
The Sprockets depend_on directive is used to declare these kinds of dependencies. So at the top of your css.scss.erb file, with the other directives (require and friends), put something like:
//= depend_on "/path/to/colors.rb"
Then when the file /path/to/colors.rb changes, it will force the css to update too.
Unfortunately, I have never gotten this to work with a relative path to a file outside of one of the asset directories (javascripts/stylesheets/images) so there may be something in the way Sprockets resolves paths that prevents this, or else I'm missing something. That leaves you with the options of specifying an absolute path, which will almost certainly not work in across all your app environments, or putting the constants file into your asset directories (app/assets/stylesheets/colors.rb, for example).
For reference, here's the doc for the depend_on directive from the Sprockets (2.0.3) source, in sprockets/directive_processor.rb
# Allows you to state a dependency on a file without
# including it.
#
# This is used for caching purposes. Any changes made to
# the dependency file will invalidate the cache of the
# source file.
#
# This is useful if you are using ERB and File.read to pull
# in contents from another file.
#
# //= depend_on "foo.png"
#
If anyone does know a way to specify relative paths to other places like config/initializers or something, please let me know!
In addition to David Faber's answer. I needed to use relative paths too.
I wanted to generate a js file with the locale dictionary, which would update if the locale files were changed:
//= depend_on "../../../config/locales/en.yml"
//= depend_on "../../../config/locales/ja.yml"
var locales = <%= locales.to_json %>;
Turns out that currently (Rails 3.2.3) relative paths only work if the relative path is also in the assets path!
So the ugly solution is to add the path in config/application.rb:
config.assets.paths.unshift Rails.root.join("config", "locales").to_s
http://guides.rubyonrails.org/configuring.html
config.assets.compile is a boolean that can be used to turn on live Sprockets compilation in production.
might want to try that, I'm not sure if its getting compiled real time though, at least it should disable the caching.
maybe try:
config.assets.digest = true
in your development config file
I try this, it work
in application.rb
config.autoload_paths += %W(#{config.root}/lib/assets_variables)
config.assets.paths << File.join(Rails.root, 'lib', 'assets_variables')
in lib/assets_variables/color.rb
module Color
def self.default
'blue'
end
end
in app/assets/stylesheets/color.css.scss.erb
//= depend_on "color.rb"
$default_color: <%= Color::default %>;
.content {
color: $default_color;
}
Formtastic help says to add next lines in application.css:
# app/assets/stylesheets/application.css
*= require formtastic
*= require my_formtastic_changes
But what i'm gonna do when it is scss? Can't find it in search engines.
theres nothing left to do for you, place your my_formtastic_changes.scss in app/assets/stylesheets. Rails will automagically compile your scss file and add it to application.css