Product Images with Active Storage
Master Rails Image Management with Active Storage
Key Technologies in This Tutorial
Active Storage
Rails' built-in framework for handling file attachments with cloud storage optimization and local disk support.
ImageMagick
Cross-platform image editing tool that enables automatic resizing, cropping, and image transformation capabilities.
Arbre
Active Admin's object-oriented HTML markup language for building customizable admin interface forms.
Environment Setup Requirements
Ensures you have the correct starting point for this exercise
Installs all required Ruby gems specified in the Gemfile
Sets up frontend asset pipeline and JavaScript libraries
Positions codebase at the exact completion point of previous exercise
Active Storage is optimized for cloud storage systems like Amazon S3 and Microsoft Azure, but works equally well with local disk storage for development environments.
Active Storage Installation Process
Install ImageMagick
Run 'brew install imagemagick' to add image processing capabilities to your system
Add image_processing Gem
Include the gem in Gemfile and run bundle to enable image transformation features
Initialize Active Storage
Execute 'rails active_storage:install' and migrate database to create required tables
Click the Products link in the navigation.
Select Edit next to any product to access the form we'll be customizing.
Open nutty > app > admin > products.rb in your code editor.
Active Admin operates on a convention-over-configuration principle: when admin files are minimal, it generates default behavior automatically. Since products.rb is essentially empty, Active Admin creates a basic CRUD interface. To add our custom file upload functionality, we need to explicitly define the form structure.
permit_params must be configured to specify which form fields are allowed for submission. This prevents unauthorized users from modifying restricted fields.
Below the commented code block, add this form structure:
# end
form do |f|
end
endWithin the form block, create input fields for all product attributes:
form do |f|
f.inputs "Product Details" do
f.input :title
f.input :sku
f.input :price
f.input :description
f.input :specs
end
f.actions
endLet's improve the user experience by making the SKU label more professional and clear:
f.input :sku, label: "SKU"Save the file and reload the Edit Product page to see your improved form.
We can enhance usability further by adding contextual help text. Update the SKU field:
f.input :sku, label: "SKU", hint: "A unique SKU for this product. Very important!"Save and reload to see the helpful hint text appear below the field.
You may notice that the price field displays increment/decrement arrows in some browsers. For decimal currency values, a standard text input provides better user experience. Modify the price field:
f.input :price, as: :string
The as: parameter allows you to override Active Admin's default field type selection, giving you precise control over form behavior.
Save the file and reload to confirm the price field now behaves as a standard text input.
Return to your code editor to add the image upload functionality.
Form Customization Features
Custom Labels
Override default field labels using the label parameter to display more user-friendly text like 'SKU' instead of 'sku'.
Helper Text
Add contextual hints below form fields using the hint parameter to guide users with important information.
Field Types
Control input types with the 'as' parameter to optimize user experience, such as using string inputs for decimal values.
Image Resize Methods
| Feature | resize | resize_to_limit |
|---|---|---|
| Behavior | Fits within exact dimensions | Respects maximum dimensions |
| Aspect Ratio | Maintained without distortion | Original proportions preserved |
| Use Case | Admin thumbnails (200x200) | Product displays (700x900) |
Image Integration Workflow
Upload via Admin
Use the file input field to upload product images through the Active Admin interface
Configure Display
Set up show page with attributes_table and image_tag helpers for proper image rendering
Replace Static Images
Update view templates to use dynamic Active Storage URLs instead of hardcoded image paths
Key Takeaways