Generate Link Previews in Rails with Microlink
In this tutorial I’ll show you how to leverage the Microlink API to generate link previews in Ruby on Rails.
Step 1: Application Set Up
rails new rails-microlink-example -d=postgresql --webpacker=stimulus
rails db:create
rails db:migrate
Step 2: Create Link Scaffold
rails g scaffold link url:string
-
Prevent null values at the database level.
rails db:migrate
-
Add validations.
Step 3. Add meta_data column to links table
rails add_meta_data_to_link meta_data:jsonb
rails db:migrate
-
Use ActiveRecord::Store to serialize data saved to the
meta_data
column. We can call these accessors anything we want to, but we’ll use the sames names that at returned from the Microlink responseWe could create a separate column for each
accessor
, but usingActiveRecord::Store
allows for greater flexibly and keeps the database simple. -
Update
link_params
to include values from themeta_data
column.
Step 4: Install and configure Microlink
yarn add @microlink/mql
-
touch app/javascript/controllers/microlink_controller.js
If you were you run
rails s
and view the console, you would see the following error: -
Update
babel.config.js
by addingsourceType: "unambiguous"
.
I found this solution be searching for the error and came across these resources:
- https://github.com/webpack/webpack/issues/4039
- https://babeljs.io/docs/en/options#sourcetype
Step 5: Save API Response to Hidden Fields
-
Update the markup in
app/views/links/_form.html.erb
. -
Build
handleChange
method inapp/javascript/controllers/microlink_controller.js
.
Now when a user enters a URL, the hidden fields will be set with the response from the Microlink API.
Step 6: Render Link Preview
-
Create a
app/views/links/_preview.html.erb
partial. -
Add the partial to
app/views/links/_form.html.erb
. -
Add the partial to
app/views/links/show.html.erb
. -
Build the
renderPreview
method inapp/javascript/controllers/microlink_controller.js
.
At this point you should be able to render a link preview.
Step 7: Attaching the Preview Image to the Link
Right now we’re not actually attaching the image to the Link
but rather we’re saving the absolute URL to the image. This means that over time those images could break, since we have no control over them. One solution is to download the image and attach it to the Link
using Active Storage.
- Run
rails active_storage:install
andrails db:migrate
to install Active Storage. -
Add
has_one_attached :thumbnail
to Link Model. - Run
bundle add down
to install the down gem. This will make downloading the remote image returned from the Microlink API easier than by doing it with native Ruby. -
Run
rails g job microlink_image_attacher
to generate an Active Job. We’ll use this Job to download and attach the image returned from the Microlink API.We add
discard_on Down::InvalidUrl
to discard any job that returns aDown::InvalidUrl
exception. This can happen if the Microlink API returns a base64 image. -
Perform MicrolinkImageAttacherJob when a Link is saved.
You could call
perform_later
instead ofperform_now
. -
Render attached thumbnail in
app/views/links/_preview.html.erb
.
Now when you save a link that returns an image, it will be saved in Active Storage.
Step 8: Handling Errors and Improving the UX
Now that we have our happy path complete, we should improve the UX to account for any errors. Most notably, when someone enters an invalid URL or if the Microlink API returns an error.
-
Add markup for rendering a message to
app/views/links/_form.html.erb
. -
Update
app/javascript/controllers/microlink_controller.js
to handle errors and render messages.