Ruby on Rails Looks for CSS in Assets Instead of Public/Stylesheets

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:

  1. file exists: app/assets/stylesheets/application.css
  2. its content looks like:
/* ...
*= require_self
*= require_tree .
*/

  1. 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 that
Rails.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 your
asset_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



Leave a reply



Submit