You have a User
and a User
has Photos
, of which one could be the Avatar
...
What I would recommend you do is have a avatar_id
on User
, and a relationship like the following:
class User < ActiveRecord::Base
has_many :photos
belongs_to :avatar, class_name: 'Photo'
end
Then in your controller
, if you preference is to add a new action
, it would be like so:
class PhotosController < ApplicationController
...
def avatar
if current_user.update_attribute(:avatar_id, params[:id])
#set flash success
else
#set flash fail
end
redirect_to(current_user) # However you plan to handle this...
end
...
end
(The reason why you might want to use a new controller is to maintain "DRY")
Which I assume you would have a member route
like so:
resources :photos do
member do
post :avatar
end
end
And an action
like so:
button_to('Set as Avatar', [:avatar, @photo])
Then you reference it in the view like so:
@user.avatar.image_url(:thumb)
That should do the trick.