What field type do I use for an avatar image when creating a table with Ruby on Rails?
I'm new to web development and Ruby on Rails, so this might be a very easy question to answer, but I can't find anything after researching.
I need to create a table which will contain a name, e-mail address and avatar image. I'm aware that the names an开发者_开发问答d e-addresses will need to be set as strings, but I'm not sure what field type to use for the image.
Can anyone help me out?
Thanks
I would suggest using CarrierWave (https://github.com/jnicklas/carrierwave) or Paperclip which simply store a reference to the file stored either locally or on something like Amazon S3.
Unless of course you REALLY want to store the image in the database - usually a BLOB/Binary date type.
In the simplest way you should store image on your File System and the path to your image in your database, so the field is STRING
.
When I deal with files I use or gems paperclip
or CarrierWave
:
- https://github.com/thoughtbot/paperclip
- https://github.com/jnicklas/carrierwave
Usefull Ryan's screencasts:
- http://railscasts.com/episodes/253-carrierwave-file-uploads
- http://railscasts.com/episodes/134-paperclip
Or generate separate STRING
field where I store path to my file.
John's answer already is the solution. As an alternative, you could go with Gravatar (www.gravatar.com).
app/helpers/application_helper.rb
def gravatar(email, size)
gravatar_id = Digest::MD5::hexdigest(email).downcase
"http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}&d=mm"
end
view
<%= image_tag gravatar(user.email, 48) %>
精彩评论