52

I have the following code:

<%= image_tag iterator.image.url(:small), :class => "img_preview" %>

But the rendered HTML shows:

<img src="/actives/hotels/13/small/clean_wave.jpg?1317675452" alt="Clean_wave">

Why the "class" attribute isn't there?

Thank you!

content01
  • 3,115
  • 6
  • 41
  • 61
  • 2
    Maybe because of your parentheses around (:small)... Try image_tag(iterator.image.url(:small), :class => "img_preview") (with parentheses all around) – Damien Nov 25 '11 at 19:41
  • Noup.... that didn't work either =S – content01 Nov 25 '11 at 20:46
  • 2
    I use in this way, here works. `<%= image_tag 'delete.png', :class => "ajax-delete" %>` `Delete` – Fernando Almeida Nov 26 '11 at 13:49
  • The way you have it should work... Something else must be going on. Can you post the rest of your view code? Do you have javascript affecting this view that could be at fault? – Andrew Nov 26 '11 at 17:58

2 Answers2

115

Your class has to be assigned inside of the brackets to be used as part of the options being passed through. Try:

<%= image_tag(iterator.image.url(:small), :class => "img_preview") %>
Simpleton
  • 6,285
  • 11
  • 53
  • 87
  • 1
    This way you're passing the `:class` option to the `url` method, which is very unlikely to work (why would the `url` method have any knowledge of what HTML tag it's being used in?). I have a feeling what you really meant was `image_tag(iterator.image.url(:small), :class => "img_preview")`. – Leo Cassarani Nov 26 '11 at 16:35
17

For newbies like myself, heres a cleaner version with the newer rails syntax:

<%= image_tag iterator.image.url(:small), class:"img_preview" %>