Ruby on Rails looks for css in assets instead of public/stylesheets
What am I doing wrong here?
Nothing, you are just using a default 3.1 install which uses the new sprockets-based asset pipeline.
put your stylesheets into /app/assets/stylesheets and use
<%= stylesheet_link_tag 'application.css' %>
in your views
the new pipeline takes all the stylesheets in that folder and automagically compiles them into a single file.
==
Alternatively, you can set turn the new pipline off in your application.rb with
config.assets.enabled = false
Ruby on rails: stylesheet in assets doesn't works
When you precompile your assets for production, make sure your development environment isn't also using those assets. So inspect your html and make sure you can see each css file and not just application.css
If you do find that you're using your precompiled assets, run rake assets:clean
and then your development environment will use the right css files.
Rails: difference between placing stylesheets under 'assets' vs. 'public'
I'd suggest you go read about the "rails asset pipeline." That should answer your questions...
http://guides.rubyonrails.org/asset_pipeline.html
http://railscasts.com/episodes/279-understanding-the-asset-pipeline
The short answer is that 'app/assets' uses the asset pipeline and 'public/stylesheets' is simply a static file.
Rails: application.css not in asset pipeline
origin answer
in my case, I forgot to install yarn command in my server.
so, please install yarn
before running rails server. otherwise assets:precompile
will do nothing and give no warning.
update
also, make sure all of these things:
- file exists:
app/assets/stylesheets/application.css
- its content looks like:
/* ...
*= require_self
*= require_tree .
*/
- also check file
app/assets/config/manifest.js
,
//= link application.css
Can't get stylesheet to show up in ruby app
use this<%= stylesheet_link_tag "/blueprint/screen" %>
& place your screen.css
in the following heirarchy:
app/assets/stylesheets/blueprint/screen.css
How can I have assets compiled into image, stylesheet, and javascript directories?
Solution #1:
Create a new subdirectory within the app/assets/
like all
and move the
current asset directories into that the new folder; e.g.
mkdir -p app/assets/all
mv app/assets/{javascripts,images,stylesheets} app/assets/all/
Then when precompiling assets with RAILS_ENV=production rails assets:precompile
it should create those javascripts
, images
and stylesheets
directories
underneath the public/assets
directory;
public/assets/javascripts
public/assets/images
public/assets/stylesheets
Solution #2:
You can create a new directory somewhere else in your project; like assets
,
instead of app/assets
; and add that the new directory to the current Rails
asset paths configuration within the assets.rb
initializer
# app/config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.application.root.join("assets")
Then precompiling assets should have the same effect as Solution #1.
Solution #3
So, as part of the Sprocket-Rails Engine it preloads the app/assets
subdirectories with this following block of code:
# ~/ruby/gems/2.3.0/gems/sprockets-rails-3.2.0/lib/sprockets/railtie.rb:54
module Rails
# [...]
class Engine < Railtie
# Skip defining append_assets_path on Rails <= 4.2
unless initializers.find { |init| init.name == :append_assets_path }
initializer :append_assets_path, :group => :all do |app|
app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
end
end
end
end
The line of interest is the
app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
It call to existent_directories
off the *paths["app/assets"]
is returning
all the subdirectories within the assets folder, hence why there are no
subdirectories in the public/assets
folder when Sprocket computes its digested
assets.
In order to add those sub-directories back in, we have to modify the current
Rails configured environment for Sprockets; i.e.Rails.application.config.configure
. However, the Sprockets::Paths
that is
included in the Sprockets::Configuration
thatRails.application.config.configure
yields to does not allow public access to
its internal @paths
variable, nor does it have an method to remove paths like
it does to add paths via its append_paths
. Instead we have to duplicate the
current paths already included by the above Railtie initializer block, remove
the old "app/assets"
subdirectories paths that we do not want, add in just the"app/assets"
directory we do want and then append them back into the configured
Sprockets environment; which looks something like this in a Rails initializer:
# app/config/initializers/assets.rb
Rails.application.config.assets.configure do |env|
old_paths = env.paths.dup
new_paths = old_paths - Rails.application.paths["app/assets"].existent_directories
new_paths << Rails.application.root.join("app", "assets")
env.clear_paths
new_paths.each { |path| env.append_path(path) }
end
Closing Comments
Using any of these solution should also mean that you will need to specify the subdirectory in all yourasset_path
method calls within your view templates in order to find the
compiled asset; e.g.
<%= asset_path "images/example.png" %>
<%= asset_path "javascripts/application.js" %>
<%= asset_path "stylesheets/application.css" %>
/assets/images/example-ca63e56ac855bdfb187479a35a7476cd65c539727f84fea19e1ad258cf3d23f5.png
/assets/javascripts/application-a4f3e75c7f7aa6d6cbc2ebcbb436b12aca442553219883805baebdd2eecd5471.js
/assets/stylesheets/application-539757f75200b6ea43399bf5e25cbc2819c1e6a610f94d5c9beaf91fec89384e.css
I hope one these solutions help.
Related Topics
How to Install and Use Slim Template Engine with Middleman
Application.CSS Not Being Served as an Asset
How to Join a Table and Count Records in Rails 3
Using Rvm, But Can't Set Current Ruby Version (Ubuntu 11.10)
Using Ruby 2.0 on Amazon Opsworks
Why Does Using '-Webkit-Appearance: None' on a Select Option in Osx Make The Text Disappear
How Do Enumerators Work in Ruby 1.9.1
Git Post-Receive Hook Not Running Bundle Install
Error When Installing Ruby 2.1.3 with Rvm
Using CSS to Style Ruby Objects in Erb
Devise Not Working Well with Multiple Subdomains on Ror3 Application
What's the Best Way to Start a Background Process, That Can Get Accessed Later On
Ruby on Rails Collection Select - How to Pre-Select the Right Value
Ruby Block and Returning Something from Block
Jekyll Templates Using Django-Like Liquid Blocks/Inheritance