Test Test
Test Test

Reputation: 2889

Failing "if" in Rails 3

I have this piece of code that doesn't seem to work for a strange reason:

    <% if (@user.photo.blank?) %>               
        <%= image_tag("empty_profile_pic.png") %> <!-- replace with user's image -->
    <%else %>
        <%= image_tag(@user.photo.url(:small)) %> 
    <% end %>

If the picture is null in the mySQL database I want it to display another pic. I have tried, empty?, nil?, blank? but with no success, and also

 @user.photo.blank.url.*

Any help?

Upvotes: 0

Views: 53

Answers (2)

Jason Noble
Jason Noble

Reputation: 3766

<%= image_tag(@user.photo.try(:url, :small) || "empty_profile_pic.png") %>

Upvotes: 1

thisfeller
thisfeller

Reputation: 798

How about

@user.photo.exists?

~Charles~

Upvotes: 1

Related Questions