Attachment_fu for an avatar

Hey everyone, so I am building a social network and am sing attachment_fu for photo uploading purposes. I've gotten this functionality to work fine. I can have users upload multiple photos and display them all. Now what is causing me trouble is trying to give each profile an avatar/picture and having this avatar replaced if a user wants a different picture. I think I need to implement some sort of exists? function, to check if a user has uploaded an avatar and if not, display a default image. Heres the code I have for the upload view for an avatar: <h1>upload an avatar</h1> <% form_for(:avatar, :url => avatars_path,                       :html => { :multipart => true }) do |f| -%>   <p>     <label for="avatar">upload an avatar:</label>     <%= f.file_field :uploaded_data %>   </p>   <p>     <%= submit_tag 'Create' %>   </p> <% end -%>

But this creates a new object, and I want to overwrite the existing object so that avatar images are not piling up in the database. Does anyone know how to do this?

The second problem if anyone has time is that I want to display a default avatar if an avatar has not been uploaded. This is a function that is part of my view for avatars: @avatar = Avatar.find(:all, :conditions => { :user_id => session[:user_id] }) Is it possible to use an if else statement and check this @avatar object to see if anything is in there? and if not display the default? So I was thinking something like this, but this does not work:

if @avatar == 0 //some logic like this?      <%= image_tag 'default.jpg'%> else   <% for avatar in @avatar -%>      <%= image_tag(avatar.public_filename(:thumb)) %>         <% end %>

If anyone has any ideas, they would be greatly appreciated. Thanks, Dave

Hi,

Hey everyone, so I am building a social network and am sing attachment_fu for photo uploading purposes. I’ve gotten this functionality to work fine. I can have users upload multiple photos and display them all. Now what is causing me trouble is trying to

give each profile an avatar/picture and having this avatar replaced if a user wants a different picture. I think I need to implement some sort of exists? function, to check if a user has uploaded an avatar and if not, display a default image.

Heres the code I have for the upload view for an avatar:

upload an avatar

<% form_for(:avatar, :url => avatars_path, :html => { :multipart => true }) do |f| -%>

upload an avatar: <%= f.file_field :uploaded_data %>

<%= submit_tag 'Create' %>

<% end -%>

But this creates a new object, and I want to overwrite the existing object so that avatar images are not piling up in the database. Does anyone know how to do this?

I don’t know anything about attachment_fu but it seems you should adjust your association to has_one or belongs_to.

The second problem if anyone has time is that I want to display a default avatar if an avatar has not been uploaded. This is a function that is part of my view for avatars: @avatar = Avatar.find(:all, :conditions => { :user_id =>

session[:user_id] }) Is it possible to use an if else statement and check this @avatar object to see if anything is in there?

if @avatar <%= image_tag ‘default.jpg’ %>

else … end

However, it appears you may way to call it @avatars as I look at your find code which will return a collection of Avatars. So I would actually do something like this.

if @avatars.size == 0

<%= image_tag ‘default.jpg’ %> else … end

ActiveSupport in Rails also provides Array#empty? so it could be written:

if @avatars.empty?

and if not display the default? So I was thinking something like this, but this does not work:

if @avatar == 0 //some logic like this?

nil and false are the only false things in Ruby.