Using avatar images for users in Rails

240 Views Asked by At

I'm looking to create some avatars for my users in a Rails app. Basically I'll have a few images as radio buttons in the User sign up form that will show the various images that the user can then select as their own.

I was wondering from people's experiences what the best way to accomplish this would be. Is it to also create an Avatar model and have the User with a has_one association to it? Or is it better to have it as just an attribute on the User model? I'm thinking that I would make a hash somewhere like { spider-hero: "spiderman.jpg", claw-hero: "wolverine.jpg", hammer-hero: "thor.jpg" } and have avatar attribute just be the hash's keys.

If anyone could help in where I would put this hash in the Rails architecture as well as how to have these radio buttons with the form that displays the image right next to each choice, that would be very helpful.

Thanks all!

How I'm envisioning it:

<%= f.radio_button :avatar, image_tag(hash[spider-hero]), checked: true %>
<%= f.radio_button :avatar, image_tag(hash[claw-hero]) %>
<%= f.radio_button :avatar, image_tag(hash[hammer-hero]) %>
1

There are 1 best solutions below

0
On

If it's just a single attribute (a filename or URL), I don't think it makes sense to create an entirely new model to store it.