Rails Images and Assets Not Being Loaded Properly

Rails images and assets not being loaded properly

I found this in edgerails documentation: http://edgeguides.rubyonrails.org/asset_pipeline.html#css-and-sass

2.3.2 CSS and Sass

When using the asset pipeline, paths to assets must be re-written and sass-rails provides -url and -path helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.

  • image-url("rails.png") becomes url(/assets/rails.png)
  • image-path("rails.png") becomes "/assets/rails.png"

The more generic form can also be used but the asset path and class must both be specified:

  • asset-url("rails.png", image) becomes url(/assets/rails.png)
  • asset-path("rails.png", image) becomes "/assets/rails.png"

Rails Javascript, images not being linked or loaded correctly

I looked at development.rb and found a line

 config.assets.prefix = "/dev-assets"

I removed the line and the stars started working. I don't know what this is and I hope nothing becomes broken because of it.

Adding New Image to Asset Path Then Rails 5 Won't Load

I finally found the solution. Never put your image to apps/assets/images/ as it will need to compile the image to set to public, all you need to do is to move all the images to public/ and then create new subfolder called assets/ or something you wish to use.

Images won't load in Rails 4 asset pipeline

I was also having the same problem. I just restarted the rails server and it worked for me.

Image not loading in dynamic page of Ruby on Rails app

If the image tag has the following exact image path specified:

<img src="assets/m1.jpg" etc="etc">

...then the browser will treat that as a relative URL and try to look it up relative to the URL of the current page, which is http://localhost:3000/posts/2.

As a result, the browser will look for an image with this URL: http://localhost:3000/posts/assets/m1.jpg, which is exactly what's happening in your case.

Try using the image_tag helper instead:

<%= image_tag "m1.jpg" %>

Rails 4: assets not loading in production

In /config/environments/production.rb I had to add this:

Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.

It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.



Related Topics



Leave a reply



Submit