0

I have this code

<div class="w-col w-col-7 w-col-stack w-hidden-small w-hidden-tiny col-spc">
     <div class="nivo-slice" style="left: 0px; width: 900px; height: 100%; opacity: 1;">
          <img src="/assets/home/toppho08.jpg" alt="toppho08.jpg">
     </div>

</div>

and it work well in development but when i tried to run it in production, images won't be displayed. So, I was told to use image_url but it html seems to not accept the code. I have this instead and it went okay.

<img src="http://sitename.com/assets/home/toppho08.jpg" alt="toppho08.jpg">

But I want to use the image_url. How do I apply image_url in html?

These links do not answer my question http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html http://awardwinningfjords.com/2009/05/11/simplify-image-paths-with-image_url.html https://ellislab.com/forums/viewthread/54731/#268424 Rails 4 not right path for image_url

Community
  • 1
  • 1
MAC
  • 676
  • 3
  • 13
  • 32

2 Answers2

3

Just use this it will work for you:

<%= image_tag 'home/toppho08.jpg' %>

or

<%= image_tag image_url('home/toppho08.jpg') %>
Gaurav Gupta
  • 475
  • 4
  • 11
  • thanks, it works...can you help me with css matter? .photo-client._4 { background-image: image-tag('home/client4.jpg'); } this won't work.. the image won't show.. i have this under my html by the way
    – MAC Apr 27 '15 at 06:05
  • 1
    use image-url in place of image-tag – Gaurav Gupta Apr 27 '15 at 06:20
  • background-image: image-url("home/client4.jpg"); <---- i did this already but still didn't work – MAC Apr 27 '15 at 06:26
  • 1
    If your css file is with the '.css' extension then this should be work : background-image: url("/assets/home/client4.jpg") – Gaurav Gupta Apr 27 '15 at 06:45
  • 1
    So in that case try to use 'asset-data-url', please check this http://stackoverflow.com/questions/15257555/how-to-reference-images-in-css-within-rails-4# – Gaurav Gupta Apr 27 '15 at 06:56
  • i tried again the image_url, it did work.. i just forgot to run this again after changing to image_url ---> rake assets:precompile RAILS_ENV=production. Thanks a lot, it's a great help for me.. please accept the votes on your comments as my thank you :) – MAC Apr 27 '15 at 07:12
1

Following is enough to use image_tag

<%= image_url 'home/toppho08.jpg' %>
jon snow
  • 3,062
  • 1
  • 19
  • 31
  • it didn't work.. this is displayed instead of the photo >>> http://localhost:3000/assets/home/toppho08-99a5359e9ccec442303bd25ffbabd45f.jpg – MAC Apr 27 '15 at 05:26
  • May be it is a assets precompiling issue, try `RAILS_ENV=production bundle exec rake assets:precompile` – jon snow Apr 27 '15 at 05:41
  • rake assets:precompile is already done and under production.rb, i alread set the config.serve_static_assets to true as well as the digest. what else could it be? – MAC Apr 27 '15 at 05:43
  • It must have to work. Now its not look like a `tag` issue. Its some production server configuration issue. Which production server you are using ? – jon snow Apr 27 '15 at 05:48
  • as of now, i tried to run it in localhost production just to test if it's going to work on server – MAC Apr 27 '15 at 05:49
  • Add `gem 'rails_12factor', group: :production` in your Gemfile, if you are using `Heroku` – jon snow Apr 27 '15 at 05:52
  • if this counts as server, i am using a personal name from the client... www.clientsbusinessname.com. Anyway, thanks a lot for your help.. image_tag worked. please accept my vote up to your answer for helping me. :) – MAC Apr 27 '15 at 05:59