If photo field blank then?

Hi all,

In my application all users have the ability to upload a profile picture
through a file field but it is not compulsory.

On my index page it displays all users along with their profile pictures
but not all users have a profile picture and therefore some users have
no image at all.

How would I go about getting a generic image to display for the rest of
the users.

I have the following section in the index:

  <% for user in @users %>
    <tbody>
      <td align = "center"><%= image_tag user.photo.url, :height => 50,
:width => 50 %></td>
      <td align = "center"><%= user.username %></td>
      <td align = "center"><%= user.favourite_console %></td>
      <td align = "center"><%= user.games.count %></td>
      <td align = "center"><%= link_to "Show", user %></body>
    </tbody>
  <% end %>

For the following line I want to do something like this:

If user has photo
          <td align = "center"><%= image_tag user.photo.url, :height =>
50, :width => 50 %></td>
else
          display this photo
end

How would I go about doing this?

Thanks

Hi all,

In my application all users have the ability to upload a profile picture
through a file field but it is not compulsory.

On my index page it displays all users along with their profile pictures
but not all users have a profile picture and therefore some users have
no image at all.

How would I go about getting a generic image to display for the rest of
the users.

Add a method to model User called image_url (for example) that returns
the actual photo url if there is one or the url of the default image
if there is not.

I have the following section in the index:

<% for user in @users %>
<tbody>
<td align = "center"><%= image_tag user.photo.url, :height => 50,
:width => 50 %></td>

Then here use image_tag user.image_url

Always move things down to the model if possible. This has the
additional advantage that you can test it in the model unit test which
is much easier than testing it in the view.

Colin

Hi all,

In my application all users have the ability to upload a profile picture
through a file field but it is not compulsory.

On my index page it displays all users along with their profile pictures
but not all users have a profile picture and therefore some users have
no image at all.

How would I go about getting a generic image to display for the rest of
the users.

I have the following section in the index:

<% for user in @users %>
   <tbody>
     <td align = "center"><%= image_tag user.photo.url, :height => 50,
:width => 50 %></td>
     <td align = "center"><%= user.username %></td>
     <td align = "center"><%= user.favourite_console %></td>
     <td align = "center"><%= user.games.count %></td>
     <td align = "center"><%= link_to "Show", user %></body>
   </tbody>
<% end %>

For the following line I want to do something like this:

If user has photo

<%- if user.photo.url? -%>

         <td align = "center"><%= image_tag user.photo.url, :height =>
50, :width => 50 %></td>
else
         display this photo

<%= image_tag 'generic_avatar.png' %>

end

How would I go about doing this?

Ideally, you would extract this into a helper method, since you don't want to be repeating all that conditional logic all over the place. Then you could simply call the 'avatar' method:

#users_helper.rb

def avatar(user)
  if user.photo.url?
    raw image_tag(user.photo.url)
  else
    raw image_tag('generic_avatar.png')
  end
end

Untested, but it ought to work.

Walter

Walter Davis wrote in post #1049293:

the users.
     <td align = "center"><%= link_to "Show", user %></body>
   </tbody>
<% end %>

For the following line I want to do something like this:

If user has photo

<%- if user.photo.url? -%>

         <td align = "center"><%= image_tag user.photo.url, :height =>
50, :width => 50 %></td>
else
         display this photo

<%= image_tag 'generic_avatar.png' %>

end

How would I go about doing this?

Ideally, you would extract this into a helper method, since you don't
want to be repeating all that conditional logic all over the place. Then
you could simply call the 'avatar' method:

#users_helper.rb

def avatar(user)
  if user.photo.url?
    raw image_tag(user.photo.url)
  else
    raw image_tag('generic_avatar.png')
  end
end

Untested, but it ought to work.

Walter

Hey Walter, I placed the above in my user helper and my application
helper but no luck> It doesn't change anything. I even changed the above
to def photo(user)

I tried changing my index.html bit from what it is to image_tag
photo.url(user) but that caused an error.

I am not sure now, I tried using the old code I had for gravatar but
that doesn't do anything.

Thanks
Christopher Jones

I think my suggestion was better. If you can put something in the
model it is generally better than in a helper.
But to use Walter's method, did you change the code in the view to use
the helper method?

Colin

Walter Davis wrote in post #1049293:

the users.
    <td align = "center"><%= link_to "Show", user %></body>
  </tbody>
<% end %>

For the following line I want to do something like this:

If user has photo

<%- if user.photo.url? -%>

        <td align = "center"><%= image_tag user.photo.url, :height =>
50, :width => 50 %></td>
else
        display this photo

<%= image_tag 'generic_avatar.png' %>

end

How would I go about doing this?

Ideally, you would extract this into a helper method, since you don't
want to be repeating all that conditional logic all over the place. Then
you could simply call the 'avatar' method:

#users_helper.rb

def avatar(user)
if user.photo.url?
   raw image_tag(user.photo.url)
else
   raw image_tag('generic_avatar.png')
end
end

Untested, but it ought to work.

Walter

Hey Walter, I placed the above in my user helper and my application
helper but no luck> It doesn't change anything. I even changed the above
to def photo(user)

I tried changing my index.html bit from what it is to image_tag
photo.url(user) but that caused an error.

IF you created the helper method I posted earlier, you would want to replace your entire image tag code in your index.html with

<%= avatar(user) %>

But I like Colin's idea even better, since it separates concerns more neatly.

#models/user.rb
...
def user_photo
  (photo.url?) ? photo.url : 'default.png'
end

And then in index.html.erb:

<%= image_tag user.user_photo %>

Much cleaner that way, since you don't have to look in the helper for the HTML or the accessor logic, rather you look for HTML in the view and logic in the model, as Rails intends.

Walter

#models/user.rb
...
def user_photo
  (photo.url?) ? photo.url : 'default.png'
end

And then in index.html.erb:

<%= image_tag user.user_photo %>

Hi,

I done the above but I get the following error:

undefined method `url?' for
/photos/original/missing.png:Paperclip::Attachment

Any ideas why this? must I change it to photo.url.present?

Thanks

#models/user.rb
...
def user_photo
(photo.url?) ? photo.url : 'default.png'
end

And then in index.html.erb:

<%= image_tag user.user_photo %>

Hi,

I done the above but I get the following error:

undefined method `url?' for
/photos/original/missing.png:Paperclip::Attachment

Any ideas why this? must I change it to photo.url.present?

Yes, or maybe photo.url.blank? or just photo.url (without the question mark) would work.

Walter

Walter Davis wrote in post #1049458:

Hi,

I done the above but I get the following error:

undefined method `url?' for
/photos/original/missing.png:Paperclip::Attachment

Any ideas why this? must I change it to photo.url.present?

Yes, or maybe photo.url.blank? or just photo.url (without the question
mark) would work.

Walter

Hey Walter.

If I put the line:

(photo.url.blank?) ? photo.url : 'images/guest.png'

They all display the guest image, even those users who have a display
picture.

If I put the line:

(photo.url.present?) ? photo.url : '/images/guest.png'

The users who have images have their photos displayed but those who
don't get a blank image.

Any ideas?

Unless you have a sub-folder named images inside your images folder, you need to just pass guest.png to the image_tag helper (it already assumes that your pictures are in the images folder).

Walter

I have had no luck :frowning:

I have got the following in my model

  def user_photo
  (photo.url.present?) ? photo.url : 'guest.png' #tried with just
photo.url
  end

and the following my in index

<%= image_tag user.user_photo, :height => 50, :width => 50 %>

I don't know what is stopping it from displaying the guest.png because
if I changed the line to photo.url.blank? then it will display the
guest.png for every user but with the present or simply photo.url line
it just doesn't display the guest.png for any of them.

Thanks
Christopher Jones

I have had no luck :frowning:

I have got the following in my model

def user_photo
(photo.url.present?) ? photo.url : 'guest.png' #tried with just
photo.url
end

and the following my in index

<%= image_tag user.user_photo, :height => 50, :width => 50 %>

I don't know what is stopping it from displaying the guest.png because
if I changed the line to photo.url.blank? then it will display the
guest.png for every user but with the present or simply photo.url line
it just doesn't display the guest.png for any of them.

Could you put just this in your view (replacing your current image tag):

<%= user.user_photo %>

And see what prints out in the browser. My guess is that it will be something, but I don't know what that is. Also, are you seeing any errors in the console when you test this locally?

Walter

Use rails console to work out what is going on. In the console 'find'
a record with no url and then you can call your method and see what it
returns, also you can call photo.url.present? and so on to see what
they give.

Colin

Walter Davis wrote in post #1049473:

<%= image_tag user.user_photo, :height => 50, :width => 50 %>

I don't know what is stopping it from displaying the guest.png because
if I changed the line to photo.url.blank? then it will display the
guest.png for every user but with the present or simply photo.url line
it just doesn't display the guest.png for any of them.

Could you put just this in your view (replacing your current image tag):

<%= user.user_photo %>

And see what prints out in the browser. My guess is that it will be
something, but I don't know what that is. Also, are you seeing any
errors in the console when you test this locally?

Walter

Hey Walter, good shout.

I just checked my console and I see this for the images section for
those users who don't have an image:

Started GET "/photos/original/missing.png" for 127.0.0.1 at 2012-02-29
15:29:16 +0000

ActionController::RoutingError (No route matches [GET]
"/photos/original/missing.png"):

I don't know where it is getting that line from.

Thanks

Walter Davis wrote in post #1049473:

<%= image_tag user.user_photo, :height => 50, :width => 50 %>

I don't know what is stopping it from displaying the guest.png because
if I changed the line to photo.url.blank? then it will display the
guest.png for every user but with the present or simply photo.url line
it just doesn't display the guest.png for any of them.

Could you put just this in your view (replacing your current image tag):

<%= user.user_photo %>

And see what prints out in the browser. My guess is that it will be
something, but I don't know what that is. Also, are you seeing any
errors in the console when you test this locally?

Walter

Hey Walter, good shout.

I just checked my console and I see this for the images section for
those users who don't have an image:

Started GET "/photos/original/missing.png" for 127.0.0.1 at 2012-02-29
15:29:16 +0000

ActionController::RoutingError (No route matches [GET]
"/photos/original/missing.png"):

I don't know where it is getting that line from.

Aha! That is coming from deep in the heart of Paperclip. You have two options. First, put your missing icon image at that path in your application. Second, choose a different method to test on your model. Instead of using the image accessor, look for one of the actual database columns in your model. (Look in db/schema.rb at your column names, pick something like photo_file_name or the local equivalent.) Then make the test in the method something to match:

  (user.photo_file_name?) ? photo.url : 'default.png'

The problem, as I should have picked up on from your earlier mention of the missing photo never appearing, is that photo.url always returns *something*, so it's not a good test subject for the ternary operator.

Walter

Walter Davis wrote in post #1049480:

Good one. Then you can also remove the whole custom accessor method, because any request of photo.url will always return something useful now. Just go back to where you started, with image_tag user.photo.url.

Walter