How do I display an avatar in Rails
I am creating a blog where the author has there own page of titles they have written and would like to display an avatar and email addres开发者_StackOverflows. How would i go about doing this?
Thank you
One solution is to use Paperclip. It's very simple to use and you can display the same picture in multiple sizes. For instance, similarly to SO you can show smaller avatars when on a question page, and bigger avatars when you're on a user's info page.
Example
class User < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :large_avatar => "300x300>",
:small_avatar => "100x100>" }
end
You would only need to add that to your model, and then a few extra columns for the migration. My point is that there's not a lot of overhead in using the solution. You will have to include a few other parameters for the forms you use to submit a file url, etc., but it's really simple.
http://gravatar.com has grown in popularity recently, it is used on stackoverflow and github.
You use an md5 hash of the email address to show the avatar.
Usage:
require 'digest/md5'
"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest("Email Address".downcase)}?s=128"
Result:
http://www.gravatar.com/avatar/012f4052c6fb1a600a3e4f39e1f2439a?s=128
If you have their email address information, I'd recommend using Gravatar.
In essence, you create an md5 hash of their e-mail address (after converting it to all lower-case characters) and use that to build an image URL.
They have plugins for many blog engines, and it's very straight forward to implement without a plugin. See their Developer Resources page.
精彩评论