1

I am using paperclip in my rails app. I want to use an image uploaded via paperclip if an attribute appears for the field and use another image if it doesn't. With this code, the first portion of the if statement always executes regardless of whether there is an entry in the logo field. Any advice?

        <% if @product.logo.url %>
            <%= image_tag @product.logo.url, :style => 'width:60px;' %>
        <% else %>
            <img src = '/assets/logo/<%= @product.slug.downcase %>-sq.jpg' class="img-circle" style = 'width:60px;'>
        <% end %>

1 Answer 1

3

You probably want to try calling String#blank?:

<% if @project.logo.url.blank? %>
    <%= image_tag @product.logo.url, :style => 'width:60px;' %>
<% else %>
    <img src = '/assets/logo/<%= @product.slug.downcase %>-sq.jpg' class="img-circle" style = 'width:60px;'>
<% end %>
Sign up to request clarification or add additional context in comments.

5 Comments

I've tried both empty and nil and they both don't seem to work. It seems to go to the second part of the if/else statement.
.blank? checks for both empty string and nil.
Blank executes the 2nd part of the if/else only as well.
@SergioTulentsev blank would be better, I'll update my answer.
@sharataka are you sure the url is empty? have you tried seeing what the value of the url is?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.