百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

active-storage-base64

> 后端框架
开源

ActiveStorage 的 Base64 支持

164 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

ActiveStorage 的 Base64 支持

ActiveStorageBase64

Adds support for base64 attachments to ActiveStorage.

Installation

In order to get the gem working on your project you just need to add the gem to your project like this:

ruby
gem 'active_storage_base64'

Compatibility

Rails Version ActiveStorageBase64 Version
8.1.x 3.0.x
8.0.x 3.0.x
7.2.x 3.0.x
7.1.x 3.0.x
7.0.x 2.0.x
6.1.x 1.2.x
6.0.x 1.1.x
5.2.x 0.1.x

Prerequisites

The only prerequisite for using this gem is having ActiveStorage properly set up in your project. For more information on how to do this, check Active Storage Overview.

Usage

In order to use the gem's functionality, you need to include the ActiveStorageSupport::SupportForBase64 module in your ActiveRecord models. For example:

ruby
class User  'data:image/png;base64,[base64 data]'
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end
ruby
class UsersController  { data: 'data:image/png;base64,[base64 data]' }
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end

  def avatar_params
    params.require(:avatar).permit(:data)
  end
end
ruby
class UsersController  'data:image/png;base64,[base64 data]'
    user.save
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end

Specifying a filename or content type

If you are willing to add a specific filename to your attachment, or send in a specific content type for your file, you can use data: to attach the base64 data and specify your filename:, content_type: and/or identify: hash keys. Check the following example:

ruby
class UsersController  'data:image/png;base64,[base64 data]'
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end

Or, in case you want to have the avatar attached as soon as the user is created you can do:

ruby
class UsersController < ApplicationController
  def create
    user = User.create(user_params)
  end

  private

  def user_params
    params.require(:user).permit(:username, :email, avatar: [:data,
                                                             :filename,
                                                             :content_type,
                                                             :identify])
  end
end

Data Format

To attach base64 data it is required to come in the form of Data URIs . For example:

data:image/png;base64,[base64 data]

Avoid bloating logs

Because we use strings to pass data in params, if you are not careful, these long strings will show up in your production logs, causing a lot of bloat. To avoid this, filter your base64 params:

diff
# config/initializers/filter_parameter_logging.rb

- Rails.application.config.filter_parameters += [:password]
+ Rails.application.config.filter_parameters += [:password, :avatar, :pictures, :file]

Contributing

Please read our CONTRIBUTING and our CODE_OF_CONDUCT files for details on our code of conduct, and the process for submitting pull requests to us.

Author

Ricardo Cortio

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

Special thanks to the people who helped with guidance and ensuring code quality in this project: Santiago Bartesaghi, Santiago Vidal and Matias Mansilla.

Credits

Active Storage Base64 is maintained by Rootstrap with the help of our contributors.

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyactivestorageapihacktoberfestrails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架