表单: RailsNotes
用户: dreamable
创建日期: 2021-04-21 07:42:50 UTC
更新日期: 2021-04-30 10:17:05 UTC
引用:(Table ID 3, Record ID 40)

标题 :
Rails ActiveStorage
笔记 :

Guide[Rails 6.0]

  • 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

    • in models/user.rb
    has_one_attached :avatar
    
    • in views
    <%= f.file_field :avatar,  class: "form-control" %>
    
    • in 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
    
    • show the avatar
    <%= image_tag @user.avatar, style: 'width:20%;height:auto' if @user.avatar.attached? %>
    
    • Once a file uploaded, it's located in storage directory, e.g. storage/ky/b0/kyb05r52e6l8kr6l4pfytggq6lks
    • In config/deploy.rb, add storage to linked_dir
  • FileCenter

    • Folder Model: name, note, parent (for nested, could be null).
    • Document Model: folder(null for root), note, has_one_attached:file
    • Folder Controller & Views
      • index: for showing root directory folders and documents,
      • show: for showing specific directory folders and documents
      • new: for new folder
      • update: for rename
      • destroy: for delete, destroy recursively by model.
    • Document Controller & Views
      • new: for upload file. Redirect to folder
      • update: for rename note ? Redirect to folder
      • destroy: delete. Redirect to folder
    • Move document/folder to other folders
      • A folder can't move to its children and so on
      • A document can be moved to any folder
    • Upload multiple files
    • Download directory
    • Upload directory: TODO
标签: