Hi ruby folks,
It’s all new shining Rails 5.2.0 with shiny new features. The major feature of this release is its very own solution for file uploads handling(ActiveStorage), CarrierWave, Paperclip, Shrine are story of past now- though they are damn good at achieving what they are build for.
ActiveStorage a child of Basecamp 3, framework. Lets look at it to add it to your awesome rails app.
To create a db migration just run following commands in terminal,
this will create two tables in under-laying db.
These are the basics, lets dead-simply see it in action.
run a scaffolding for a model creation
rails g scaffold User name:string gender:string
# app/models/user.rb class User < ApplicationRecord has_one_attached :avatar end
Controller Changes –
# app/controllers/users_controller.rb def params.require(:user).permit(:name, :gender, :avatar) end
Views Changes –
_form.html.erb add below code above submit button
<!-- app/views/users/_form.html.erb -->class="field"> %= form.label :avatar %> %= form.file_field :avatar %>
Views Changes – show.html.erb
<!-- app/views/users/show.html.erb --> <% if @user.avatar.attached? %> <!-- @user.image.present? will always return true, use attached? to check presence --> <p> <strong>User Avatar:</strong><br> <%= image_tag @user.avatar %> </p> <% end %>
Grrrrrr……. Wasn’t it DeadSimple?
We’ll DeadSimply look at #multiple images, #Handlind N+1 Problem later some day. Follow and share to keep update.