Tag Archives: Paperclip

Removing Paperclip attachments

Paperclip’s GitHub page includes the necessary steps to delete an attachment – the model’s attribute that refers to the attachment simply gets set to nil. Here is the full implementation of deleting a user’s profile picture in a Rails app. Start with tests (right?!):

spec/features/editing_users_spec.rb:

require 'spec_helper'

feature 'Editing Users' do
  let(:user) { FactoryGirl.create(:user) }
  let(:admin) { FactoryGirl.create(:admin) }

  scenario 'removing user profile photo' do
    sign_in_as admin # of course
    visit edit_user_path(user)
    click_link 'Remove profile photo'

    expect(page).to have_content 'User profile photo has been removed.'
    expect(page).not_to have_css("img[src*='user.png']") # file name of the user photo defined in the factory
    expect(page).to have_css("img[src*='paperclip_default.png']") # specified in the User model
  end
end

Continue reading

Advertisements
Tagged , ,

Paperclip attachments in RSpec factories

Assuming one has a model whose attachments are handled by Paperclip, i.e.

$ rails generate paperclip user photo

app/models/user.rb:

class User < ActiveRecord::Base

  has_attached_file :photo
end

then to generate a test model with FactoryGirl, including the attachment: Continue reading

Tagged , , ,