0

Very simple question really, but it's driving me nuts.

I have this method call in a Rails view:

<%= get_image(@document) %>

The method in here returns an object of type ActiveSupport::SafeBuffer. If I call .to_str on it in a console, I see the expected result (just an html img tag). However, when I stick the above code in a view, there's nothing there. Just an empty string.

If I do this:

<%= get_image(@document).to_str %>

in an effort to convert the SafeBuffer into a String object, I see the literal HTML in the page, and not the image tag.

I feel like I'm missing something simple or elementary here. Can anyone point the way? Thanks in advance!

EDIT: since it's been requested, here's what the get_image method looks like:

def get_image(document)
  document[document.type+'.image'].nil? ? nil : document[document.type+'.image'].as_html_safe
end
bitops
  • 4,022
  • 3
  • 26
  • 31
  • show the method body of `get_image`. – Arup Rakshit Oct 16 '15 at 02:19
  • bitops, given the console has the text, and using #to_str in erb shows the literal html, it seems it really is a SafeBuffer that should be output into the code. A tag wouldn't show on the rendered page, but are you certain it isn't in the html source code? – Brian Oct 16 '15 at 14:02
  • @ArupRakshit the content of the get_image method is really irrelevant as far as I've been able to determine. – bitops Oct 16 '15 at 17:33
  • @BrianMurphy-Dye I have checked and I am certain that the html is not there. – bitops Oct 16 '15 at 17:33
  • That is truly weird! Is this rendered if you add this in the same place? <%= '

    am i rendered?

    '.html_safe %> -- that too should be a SafeBuffer.
    – Brian Oct 16 '15 at 17:38
  • @bitops I wanted to see how you are creating an object of `SafeBuffer` class. and also wanted to see if it is `html_safe` or not. – Arup Rakshit Oct 16 '15 at 17:45
  • @BrianMurphy-Dye will give it a shot, thanks for the suggestion. – bitops Oct 16 '15 at 20:44
  • @ArupRakshit it is coming from a library I am using. Calling `html_safe?` on it returns true. – bitops Oct 16 '15 at 20:44
  • @BrianMurphy-Dye just tried it out and it does work -- it's rendered correctly. – bitops Oct 16 '15 at 20:47
  • @BrianMurphy-Dye here is something I just noticed -- when I call `to_str` the reason the HTML is showing up as literal text is because it is wrapped in double quotes! – bitops Oct 16 '15 at 20:57
  • Does get_image(@document).class return SafeBuffer and get_image(@document).to_str.class return String? – Brian Oct 16 '15 at 23:30
  • @BrianMurphy-Dye yep, that's what they return. – bitops Oct 19 '15 at 17:43
  • You mentioned that `to_str` against your get_image call has double quotes around it. Does this also have double quotes around it: '

    am i rendered?

    '.html_safe.to_s? Also, what exactly does the get_image call give you?
    – Brian Oct 19 '15 at 19:33

0 Answers0