How to hide an image if it's not relevant

Posted on 2010-03-28
Medium Priority
Last Modified: 2013-11-13
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
<%= link_to image_tag(@post.youtube_img), @post.youtube_src, :class => "youtube" %>

 def youtube_img

   def youtube_src

    <%= f.label :code, "Youtube code" %><br />
		<%= f.text_field :code, :size => 20 %>

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

Open in new window

Question by:depassion
1 Comment
LVL 12

Accepted Solution

cminear earned 2000 total points
ID: 29114458
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


Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month6 days, 18 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question