Commit 590377a0
Changed files (3)
app
controllers
models
spec
controllers
app/controllers/avatars_controller.rb
@@ -3,15 +3,12 @@ class AvatarsController < ApplicationController
before_filter :find_or_build_avatar
def create
- @avatar.avatar = params[:avatar][:avatar]
- if @avatar.save
- redirect_to(profile_path(current_user), :notice => 'Your new avatar has been uploaded.')
- else
- flash[:error] = "could not upload photo"
- end
+ @avatar.attach(params[:avatar][:avatar])
+ redirect_to(profile_path(current_user), :notice => 'Your new avatar has been uploaded.')
end
protected
+
def find_or_build_avatar
if current_user.avatar == nil
@avatar = current_user.avatar = Avatar.new
app/models/avatar.rb
@@ -1,4 +1,9 @@
class Avatar < ActiveRecord::Base
belongs_to :user
mount_uploader :avatar, AvatarUploader
+
+ def attach(file)
+ self.avatar=file
+ self.save
+ end
end
spec/controllers/avatars_controller_spec.rb
@@ -13,11 +13,16 @@ describe AvatarsController do
it "should save the new avatar" do
Avatar.last.should_not be_nil
+ Avatar.last.avatar.should_not be_blank
end
it "should redirect to the profile page" do
response.should redirect_to(profile_path(user))
end
+
+ it "should display a flash notice" do
+ flash[:notice].should_not be_nil
+ end
end
end
end