ActiveStorage. 英文书Learnrails5.2的案例,看如何放到云上。

ActiveStorage. 英文书Learnrails5.2的案例


本例子目标:增加一个avatar image给每个用户。 

准备:

需要安装Imagemagick software。 它可以create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats 。

可以使用打包管理软件homebrew安装brew install imagemagick。

在Rails,需要使用gem 'mini_magick' 。通过这个gem来使用Imagemagic的功能。

⚠️,我使用80template的时候,没有用这个gem,也能正常使用。 

开始:

安装ative_storage:  rails active_storage:install 

提示从active_storage迁移:

class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
  def change
    create_table :active_storage_blobs do |t|
      t.string   :key,        null: false
      t.string   :filename,   null: false
      t.string   :content_type
      t.text     :metadata
      t.bigint   :byte_size,  null: false
      t.string   :checksum,   null: false
      t.datetime :created_at, null: false
      t.index [ :key ], unique: true
    end
    create_table :active_storage_attachments do |t|
      t.string     :name,     null: false
      t.references :record,   null: false, polymorphic: true, index: false
      t.references :blob,     null: false
      t.datetime :created_at, null: false
      t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
    end
  end
end

解释:

 t.index [:key], unique:true使用了index()方法,其内部使用了@base.add_index()方法,这个方法会生成sql语句,和数据库交互:

CREATE UNIQUE INDEX active_storage_blobs_key_index ON active_storage_blobs(key)

index的名字是表名+列名+_index后缀,太长的话,可以使用便名:add_index(name:""

解释: 

t.references :blob使用了关联方法add_references()方法,选项null:false是不得为空。

还有:type, :foreign_key, :index等方法。

type默认是digint长整数。

foreign_key:true用于本列被设置为关联外键。

index:true调用add_index方法给这个列增加索引。 



新增的两个数据表会储存所有信息。user表无需增加一个额外的列,只需在User model中建立一个关联使用ActiveStorage::Attached#Marcos#has_one_attached 宏方法。

这里has_one_attached :avatar 

然后就可以使用avatar方法了 

解释:

Macros是宏命令的简写 Macroinstruction。

除此之前还has_many_attached(), 这里ActiveStorage会自动映射recoreds和the attachments。其实这个方法内部使用了has_many关联了attachments,使用了has_many-through关联了bolbs。 

在控制台建立一个user,然后:

> user.avatar.attach(io: File.open("/Users/chentianwei/Desktop/1.jpeg"), filename: "1.jpeg", content_type: "image/jpeg") 

#attach()方法可以用参数params[:avatar]或者ActiveStorage::blob object

解释:

  1. 首先从Attachment中找到关联的user.
  2. 文件信息上传,并自动生成一个key 
  3. 把文件插入到Blob数据表中。包括文件名,类型,大小,key等信息。
  4. 如果1中没有现存的记录,此时在Attachment中建立记录,关联上user以及blob。
  5. user的updated_at属性更新。

  

可以使用user.avator.attached?  检测是否成功,返回true.

在views/users/show.html.erb增加这个图像。

<% if @user.avatar.attached? %>
  <p>
    <%= image_tag(url_for(@user.avatar), size: "200")%>
  </p>
<% end %>

在控制器端修改create和update:例子:

  def update
    avatar = params[:user][:avatar]
    respond_to do |format|
      if @user.update(user_params)
        if avatar
          @user.avatar.attach(avatar)
        end

在_form.html.erb中增加图片的上传操作:

<div>

   <%= form.label :avatar %>

   <%= form.file_field :avatar %> 

</div> 


增加了一个功能测试:测试文件上传是否成功:

attach_file("Avatar",  "#{Rails.root}/spec/files/1.jpeg"

还要在spec_helper中设置删除测试数据库中的文件具体见: 

https://www.cnblogs.com/chentianwei/p/9071330.html 


以上是基础,Active Storage还也可resize图片尺寸,可以把图片保存在云端的。具体看指导,和ruby-china的帖子。

可能先需要云服务器的知识,以及部署产品环境。 


原文地址:https://www.cnblogs.com/chentianwei/p/9169049.html