How to hide an image if it's not relevant

i have a youtube video field in a posts model. Some posts the user will add video to, some only text. For the video i display the youtube image with a link to the video. When there is no video i dont eant to display an image obviously. How can i hide it if no video code is present.
#link to the video
#app/views/posts/show/html.erb
<%= link_to image_tag(@post.youtube_img), @post.youtube_src, :class => "youtube" %>

#app/models/post.rb
 def youtube_img
     "http://img.youtube.com/vi/#{code}/default.jpg"
   end

   def youtube_src
     "http://www.youtube.com/v/#{code}&hl=nl&fs=1&rel=0&hd=1&autoplay=1"
   end

#app/views/posts/_form.html.erb
<p>
    <%= f.label :code, "Youtube code" %><br />
		<%= f.text_field :code, :size => 20 %>
  </p>

##i have tried
<% if @post.code %>
 <%= link_to image_tag(@post.youtube_img),@post.youtube_src, :class => "youtube" %>
<% end -%>

Open in new window

depassionAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cminearCommented:
Unless you are doing extra work, @post.code is likely an empty string in the cases where you aren't saving anything to it.  And with Ruby, anything that is not nil or false will be considered good enough to do what is in the 'if' expression (as you found).

Try the following code instead.  By using the "blank?" method which Rails provides, you can check for both @post.code being either nil or an empty string, and if either is the case, do not run the code to create the link.

As a separate comment: are you untainting the string submitted by the user to be saved and used for the 'code' field?  In other words, are you doing anything to make sure what is saved to 'code' is safe to use when you call youtube_img and youtube_src?  If not, you could be providing a means for cross-site scripting attacks.  (After some thought, I may be a bit too paranoid; link_to and/or image_tag may do the work to escape any bad characters as part of their operation, but I don't know that for certain personally, and I guess I'd want to know that before I ran code like you have.)  Just an added comment.
<% unless @post.code.blank? %>
 <%= link_to image_tag(@post.youtube_img),@post.youtube_src, :class => "youtube" %>
<% end -%>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Ruby

From novice to tech pro — start learning today.