Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

active-storage-base64

> 后端框架
Open source

Base64 support for ActiveStorage

164 stars0 likes1 views
WebsiteGitHub

About

Base64 support for ActiveStorage

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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactivestorageapihacktoberfestrails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

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