0

I want my image to be the status' name ("ok.png" for example).

I've tried:

link_to '<img src="images/#{refactoredStatus}.png">'.html_safe, '#', :class => 'option show_status'

But that literally shows images/#{refactoredStatus}.png in my HTML.

How do I do this?

1
  • 1
    Hint: How does 'string from single quote literal' differ from "string from double quote literal"? See some applicable reference material. Commented Dec 29, 2013 at 21:55

2 Answers 2

1

Try:

link_to image_tag("#{refactoredStatus}.png"), '#', :class => 'option show_status'

This uses the Rails image_tag helper instead.

Edit:

If you're set on using HTML instead of the image_tag helper then you can do this:

link_to "<img src=\"images/#{refactoredStatus}.png\">".html_safe, '#', :class => 'option show_status'

or this:

link_to %{<img src="images/#{refactoredStatus}.png">}.html_safe, '#', :class => 'option show_status'

I personally prefer the last solution because you don't have to escape the quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

While this does indeed use the image_tag helper, which is good, this isn't the change that "solves" the problem/issue.
-1

Ruby is reading that as a string. The proper way to do this is using the image tag, so:

<%= link_to image_tag('foobar.png'), '#' %>

http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag

1 Comment

While this does show the usage of image_tag, it doesn't address how to interpolate the variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.