gem 'image_processing', '~> 1.2'
bundle install
class Example < ApplicationRecord
has_one_attached :image
end
<%= form_with(model: @example, local: true) do |form| %>
<%= form.label :image %>
<%= form.file_field :image %>
<%= form.submit %>
<% end %>
class ExamplesController < ApplicationController
def create
@example = Example.new(example_params)
if @example.save
redirect_to @example, notice: 'Example was successfully created.'
else
render :new
end
end
private
def example_params
params.require(:example).permit(:image)
end
end
这将使您能够将 Active Storage 图像保存到本地数据库。