Posted over 6 years ago. Visible to the public. Repeats.

Whitelist Carrierwave attributes correctly

Say you have a User with a Carrierwave attribute #avatar:

class User < ActiveRecord::Base mount_uploader :avatar, AvatarUploader end

When whitelisting the avatar field in the controller, you might do this:


But you probably want this:

params[:user].slice(:avatar, :avatar_cache, :remove_avatar)

In this example:

  • :avatar_cache allows a newly upload image to persist through form roundtrips in the case of validation errors (something that isn't possible with Paperclip).
  • :remove_avatar is the name of a check box that would delete the avatar. Note that if your Carrierwave image is held by a record that only exists to hold an image (e. g. Image and Image#file, you probably want to destroy the entire record instead of leaving an empty shell behind.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Henning Koch
Last edit:
almost 3 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more