Setup
rails active_storage:install
rails db:migrate
# in config/storage.yml, setup cloud if you want, I just want to use local first
# in environments/development.rb[production.rb], set
config.active_storage.service = :local
# need public:true for local? Not work, error. It seems always public.
Add avatar to User
models/user.rb
has_one_attached :avatar
views
<%= f.file_field :avatar, class: "form-control" %>
controllers
# add permit to params
params.permit(:name, :password, :password_confirmation, :email, :avatar)
# remove avatar if necessary
if @user.avatar.attached? && uparams[:avatar].present?
@user.avatar.purge_later
end
<%= image_tag @user.avatar, style: 'width:20%;height:auto' if @user.avatar.attached? %>
storage
directory, e.g. storage/ky/b0/kyb05r52e6l8kr6l4pfytggq6lks
config/deploy.rb
, add storage to linked_dir
FileCenter