My app uses carrierwave and omniauth (facebook) and I have one minor issue.
- When user first logs in with Facebook account, I make his facebook profile image as his default image. (He doesn’t have to sign up with FB account or anything. When he logs in, he’s signed up to my application without providing any more information)
- When user first creates an account “locally” from my app, he can choose to upload a profile image. Therefore, there’s a
profile_imagefield in User model, andProfileUploaderfrom carrierwave is mounted on this field.
Problem: When I save facebook image url to this field, it gives me error. I think this is happening because there’s an uploader mounted to this field, and although the field is a string type, I can’t just save a URL to this field without uploading images via carrierwave.
I worked around this issue by storing the FB image url to a different field, other_profile_image, and whenever the user’s account is created from FB, I call his image from this field.
For example,
<% if @user.provider == "local" %>
<% if @user.profile_image.blank? %>
<%= link_to image_tag('default.jpg'), '/users/'+@user.id.to_s %>
<% else %>
<%= link_to image_tag(@user.profile_image_url(:thumb)), '/users/'+@user.id.to_s %>
<% end %>
<% elsif @user.provider == "facebook" %>
<%= link_to image_tag(@user.other_profile_image), '/users/'+@user.id.to_s %>
<% end %>
provider=local specifies accounts created on the application, not from other third-parties like FB
My workaround does a job, but it’s bugging me because I know there must be a better solution to handle this problem. I need to find out how to handle the following simple tasks:
- When user signs up locally, he can choose to upload a profile picture. If he doesn’t, I want to store my default image’s url to his profile image url field.
- When user uses his FB account, his facebook image should be set to his initial profile picture. Right now, I’m using two different field,
profile_imageandother_profile_imagebecause carrierwave threw errors when I tried to store URL strings into profile_image field, on which a carrierwave uploader was mounted.
I hope there is a great way to solve this issue! I appreciate any help in advance. Thank you very much.
You can upload photo from remote url.