[ActiveStorage] default file

i have just recently migrated from paperclip to active storage and one of the differences that i noticed is that active storage doesn’t provide a way to have a default file. i think that adding a helper method is inconvenient for each model and default file, and checking for the file if it is attached is also an issue that makes view files larger than it should

previously we could have a default behavior on paperclip using the following in our model:

has_attached_file :avatar, default_url: "/images/:style/missing.png"

i am proposing following to be used for active storage, however if this cannot be done and someone has a better spec please comment away.

has_one_attached :avatar, default_file: "/images/:style/missing.png" # or somewhere from assets

for obvious reasons this shouldn’t work on has_many_attached

i hope this is the correct channel for such an issue, if it is not please provide me with the correct one

sorry for the spam. but can i get some feedback related to this proposal, it will be much appreciated.

thank you!

Hi Hamad,

We believe a default is a concern closer to the view, better addressed with a view helper for example:

def user_avatar(user)

  user.avatar || default_user_avatar

end

In the case of the recent trend of dummy avatars with letters in them, that default would even depend on the user.

Then, views consistently use this helper instead of user.avatar.