22

Background image is not uploaded in my view page.showing this error.

ActionController::RoutingError (No route matches [GET] "/assets/images/control_top.png")

what can i do to resolve this problem?

Inaccessible
  • 1,560
  • 3
  • 18
  • 42

5 Answers5

27

In production env, Rails will not be responsible for serving static assets. Therefore, you are getting this error.

This is controlled by this setting in config/environment/production.rb in your application:

config.serve_static_assets = false

You can set to that true

or try this

rake assets:precompile 

command (compiles and copies images, css and js from app/assets to public/.

Sachin R
  • 11,606
  • 10
  • 35
  • 40
25

If you upgrade to a new version of Rails (Rails 4 and Rails 3.2.16 come to mind), and you suddenly start to see this error, it is likely that your stylesheet is pointing to the non-fingerprinted, non-cached version of the files. If you are using the asset pipeline, in order to take advantage of it, you need to use the new helpers that point to the fingerprinted, cached version of the files. To do so, you'll need to either embed erb in your css file, or use sass.

Incorrect (uses sass):

.class
  background: url('asset.png') no-repeat

Correct (uses sass):

.class
  background: image-url('asset.png') no-repeat

For more info, see here: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
  • 2
    This bit me after upgrading to Rails 3.2.16, so it's not just 4.x. – Mark Berry Jan 09 '14 at 00:51
  • 1
    @MarkBerry Thanks for chiming in. I'll update my answer to include that version too. – Aaron Gray Jan 09 '14 at 15:55
  • 1
    According to the docs, one can also use asset-url('yourimage.png'), so it would appear the 'image' addition is not functional - just a forced naming-convention change by syntax-fiddlers. Sure, I can run an awk script on all the thousands of lines of css in all the projects I work on, but this will likely fail in some unforeseen situation and break things. Anyone know of a gem that restores the original functionality of this and similar syntax-whim changes? – JosephK Jun 09 '16 at 10:57
7

Might help someone , i tried all the answers and forgot the most basic thing to do . Clearing the browser cache , once done i was good to go :)

Caffeine Coder
  • 1,869
  • 1
  • 17
  • 35
6

you have to run this command

rake assets:precompile
Asnad Atta
  • 3,855
  • 1
  • 32
  • 49
2

I kept using the asset pipeline, but had to change the hard coded url's that I used as follows (for my development environment):

I updated my /config/application.rb to use the asset pipeline:

config.assets.enabled = true

I changed all of my images urls to point to '/assets/image_without_old_image_directory_name.jpg'

so for example my images used to be in /public/images/xxx.jpg. I moved them to /app/assets/images/xxx.jpg. I changed the img src from /images/xxx.jpg to /assets/xxx.jpg

I ended up not needing to do the asset precompile, and simply removed all visible aspects of the asset pipeline in /public and in /tmp, and it just worked (for development).

Taylored Web Sites
  • 1,017
  • 1
  • 9
  • 15
  • changing img src from images/xxx.jpg to /assets/xxx.jpg worked for me, even though the actual path is assets/images/xxx.jpg. Thanks! – tomb Aug 29 '18 at 16:52
  • 1
    Is this still the best practice? I'm in Rails 6 and still getting No route matches [GET] – fatfrog Mar 06 '21 at 21:16