Gravei um rápido vĂdeo demostrando o funcionamento do Active Storage, o grande destaque do Rails 5.2.
$ rails new active_storage_demo -d postgresql
$ cd active_storage_demo
$ atom .
$ rails generate scaffold post title body:text
$ rails db:create db:migrate
$ rails active_storage:install
$ rails db:migrate
$ rails s
app/models/post.rb
class Post < ApplicationRecord
has_one_attached :image
end
app/controllers/posts_controller.rb
class PostsController < ApplicationController
# ...
def post_params
params.require(:post).permit(:title, :body, :image)
end
end
app/views/posts/_form.html.erb
<%= form_with(model: post, local: true, multipart: true) do |form| %>
<!-- ... -->
<div class="field">
<%= form.label :image %>
<%= form.file_field :image %>
</div>
<!-- ... -->
app/views/posts/show.html.erb
<!-- ... -->
<%= image_tag @post.image %>
<!-- ... -->