Search code examples
ruby-on-railsruby-on-rails-5asset-pipeline

Why Image_tag produces "images/..." instead of "assets/...."?


Rails version: 5.2.4

Ruby version 2.6.10

Per "image_tag" documentation, it should produce image tag with url: "/assets/...", but when I try in rails console, it produces the url "/images/..."(the screenshot below), it make my website cannot show. How can I make it works correctly?

Also the "/assets/..." url can load the image, but "images/..." cannot enter image description here


Solution

  • Because there's a lot of context loaded by Rails for those helper methods to work, you can't just include ActionView::Helpers::AssetTagHelper as you've done. Instead call the helper on the helper object in the rails console:

    [1] pry(main)> helper.image_tag("foo.jpg")
    => "<img src=\"/assets/foo-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.jpg\" />"