Nick
Nick

Reputation: 3090

Shows image path rather then the image, within one-line if..else statement

I have the following line of code:

<%= "Brand: " + (@user.activated ? "image_tag('brand1.png', class: 'branding')" : "image_tag('brand2.png', class: 'branding')") + "(#{@user.activated_at})" %>

It should show (where the image shown depends on whether user is activated):

Brand: {image} 6-19-2015

Instead of the image, it displays the literal image path (so text). How should I adjust my code to show the actual image instead?

Upvotes: 0

Views: 41

Answers (2)

Rokibul Hasan
Rokibul Hasan

Reputation: 4156

You can try following one in a single tag, with html_safe

<%= "Brand:  #{(@user.activated ? image_tag('brand1.png', class: 'branding') : image_tag('brand2.png', class: 'branding'))} (#{@user.activated_at})".html_safe %>

Or a more smarter way like

<%= "Brand:  #{image_tag(@user.activated ? 'brand1.png' : 'brand2.png' , class: 'branding')} (#{@user.activated_at})".html_safe %>

Upvotes: 1

Max Williams
Max Williams

Reputation: 32943

That's a really weird way to write some erb template code. As @mudasobwa says you have quotes around the image tags which is wrong, and adding lots of strings together in the erb tag is messy, fragile and unreadable. The two image tags are almost identical so could be dried up as well.

I would do what i think you are trying to do like so:

<% graphic =  @user.activated ? "brand1.png" : "brand2.png" %>
Brand: <%= image_tag(graphic, class: 'branding') %> (<%= @user.activated_at %>)

Upvotes: 3

Related Questions