Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Separate pictures and videos so only the one the user selects is displayed?

I am trying to write an if else statement so that when a users post a photo only the photo is displayed the same goes for videos. Right now if a users post a video a blank photo will be displayed under the video as well. My code is below. I know it has something to do with the lines:

<%= image_tag @post.image.url(:medium) %>
<%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>

I just don't know what the best way is to only show the one the user is posting. I am a beginner so please bare with me. Thanks in advance.

Post/Show.html

<div class="row">
    <div class="col-md-offset-4 col-med-8">
        <div class="panel panel-default">
        <div class="panel-heading center">
            <%= image_tag @post.image.url(:medium) %>
            <%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>
        </div>
        <div class="panel-body">
        <p><%= @post.description %></p>
        <p><strong><%= @post.user.name if @post.user %>    </strong></p>

    <% if @post.user == current_user %>
        <%= link_to edit_post_path(@post) do %>
        <span class="glyphicon glyphicon-edit"></span>
        Edit
      <% end %>
    <% end %>
    <%= link_to 'Back', posts_path %>
    </div>
 </div>

Form

<%= form_for @post, html: { multipart: true }  do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :image %>
    <%= f.file_field :image %>
  </div>

  <div class="field">
    <%= f.label :video %>
    <%= f.file_field :video %>
  </div></br>

  <div class="field">
    <%= f.label :description %>
    <%= f.text_field :description %>
  </div>

  <div class="actions">
    <%= f.submit class: "btn btn-danger btn-md" %>
  </div>
<% end %>

2 Answers

You could try something like this:

<%= image_tag @post.image.url(:medium) unless @post.image.url.nil? %>

Hi Miguel,

I tried your code and the below code however, both the photo box and video box are still showing up. If I post a photo a black video box is still displayed underneath. If I post a video a blank photo box is displayed under the video. Any ideas? Thanks for your response earlier.

       <% if @post.image.url != nil %>
        <%= image_tag @post.image.url(:medium) %>
    <% end %>

    <% if @post.video.url != nil %>
        <%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>
    <% end %>

It's probably due to @post.video.url not being nil and hence it is still generating the video tag. What happens when you:

<%= @post.video.url.inspect %>