Rails Generator Handles Active Storage Attachment Fields
Rails Active Storage allows attaching files to Active Record objects and uploading them to cloud storage services like AWS S3 or Google Cloud Storage. You can configure Active Storage attachment fields using Rails model and scaffold generators, making setup much easier.
Using Generators with Active Storage
You can configure Active Storage attachment fields using Rails model and scaffold generators by providing attachment or attachments field attributes:
Single Attachment
For a single file attachment:
$ bin/rails generate model User avatar:attachment
This command configures has_one_attached for the avatar in the User model.
Multiple Attachments
For multiple file attachments:
$ bin/rails generate model Gallery photos:attachments
This configures has_many_attached for the photos in the Gallery model.
Scaffold Generator
You can also use the scaffold generator:
$ bin/rails generate scaffold User name:string avatar:attachment
This creates:
- The model with
has_one_attached :avatar - Controllers with proper file upload handling
- Views with file input fields
- Routes and migrations
Generated Model
The generator automatically adds the appropriate Active Storage association:
class User < ApplicationRecord
has_one_attached :avatar
end
For multiple attachments:
class Gallery < ApplicationRecord
has_many_attached :photos
end
Generated Views
The scaffold generator creates views with file input fields:
<!-- app/views/users/_form.html.erb -->
<%= form_with(model: user, local: true, multipart: true) do |form| %>
<%= form.label :avatar %>
<%= form.file_field :avatar %>
<!-- Other fields -->
<% end %>
Controller Setup
The generated controller includes proper parameter handling:
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render :new
end
end
private
def user_params
params.require(:user).permit(:name, :avatar)
end
end
Displaying Attachments
In your views, you can display attachments:
<!-- app/views/users/show.html.erb -->
<% if @user.avatar.attached? %>
<%= image_tag @user.avatar %>
<% end %>
For multiple attachments:
<!-- app/views/galleries/show.html.erb -->
<% @gallery.photos.each do |photo| %>
<%= image_tag photo %>
<% end %>
Benefits
Using generators with Active Storage fields provides:
- Automatic configuration: Models and views are set up correctly
- Consistency: Ensures proper Active Storage usage across your app
- Time-saving: Reduces manual configuration
- Best practices: Follows Rails conventions for file uploads
Configuration
Remember to configure Active Storage in your environment files:
# config/environments/production.rb
config.active_storage.service = :amazon # or :local, :google, etc.
Conclusion
Rails generators now handle Active Storage attachment fields seamlessly, making it easier to add file upload functionality to your Rails applications. This feature reduces boilerplate and ensures consistent Active Storage setup.