<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Allie Stehney</title>
    <description>The latest articles on DEV Community by Allie Stehney (@alliestehney).</description>
    <link>https://dev.to/alliestehney</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F142311%2F28f2ba36-b5f6-4444-a51e-5b53b9da3f9a.jpeg</url>
      <title>DEV Community: Allie Stehney</title>
      <link>https://dev.to/alliestehney</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alliestehney"/>
    <language>en</language>
    <item>
      <title>How To Create an Image Uploader: Rails Active Storage</title>
      <dc:creator>Allie Stehney</dc:creator>
      <pubDate>Sun, 29 Sep 2019 23:45:09 +0000</pubDate>
      <link>https://dev.to/alliestehney/how-to-create-an-image-uploader-rails-active-storage-24bd</link>
      <guid>https://dev.to/alliestehney/how-to-create-an-image-uploader-rails-active-storage-24bd</guid>
      <description>&lt;p&gt;Recently, I had to figure out how to create an Image Uploader for a Ruby on Rails application. I had used &lt;a href=""&gt;Paperclip&lt;/a&gt; before, so I looked into that, but quickly discovered that Rails 5.2 has &lt;code&gt;Active Storage&lt;/code&gt; built into it. &lt;/p&gt;

&lt;p&gt;What is &lt;a href=""&gt;ActiveStorage&lt;/a&gt;? It's a built-in feature in Ruby on Rails that allows for uploading files to third-party storage (i.e., &lt;a href=""&gt;Amazon S3&lt;/a&gt;, &lt;a href=""&gt;Google Cloud Storage&lt;/a&gt;, etc.) while saving these images to Active Record objects.&lt;/p&gt;

&lt;p&gt;And, as long as you're on at least &lt;a href=""&gt;Rails 5.2&lt;/a&gt; - this is important because this is the the version of Rails that introduced &lt;code&gt;ActiveStorage&lt;/code&gt; - the process for getting up and running, for me at least, is and was incredibly smooth. &lt;/p&gt;

&lt;p&gt;To start, go into your rails application in the terminal and type: &lt;br&gt;
&lt;code&gt;rails active_storage: install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command runs a migration that adds two new tables to your database:&lt;br&gt;
&lt;code&gt;active_storage_blobs&lt;/code&gt; and &lt;code&gt;active_storage_attachments&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a helpful image I found to describe these tables:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FA2BFYr8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7to4xm7jcw1s5tnd0h7q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FA2BFYr8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7to4xm7jcw1s5tnd0h7q.png" alt="Image showing the CreateActiveStorageTables migration"&gt;&lt;/a&gt;&lt;br&gt;
(image is from &lt;a href=""&gt;this amazing article&lt;/a&gt; which you should read if you have a chance). &lt;/p&gt;

&lt;p&gt;Make sure to run &lt;code&gt;rails db:migrate&lt;/code&gt; to run the migration.&lt;/p&gt;

&lt;p&gt;Now that we're set up with the tables in our database, the next step is setting up where we want to store our files that we upload. Go ahead and create this file:&lt;br&gt;
&lt;code&gt;config/storage.yml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In it, we need to specify where we want to store our files that we upload for our &lt;code&gt;development&lt;/code&gt; environment, our &lt;code&gt;test&lt;/code&gt; environment, and our &lt;code&gt;production&lt;/code&gt; environment.&lt;/p&gt;

&lt;p&gt;The file should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local:
  service: Disk
  root: &amp;lt;%= Rails.root.join("storage") %&amp;gt;

test:
  service: Disk
  root: &amp;lt;%= Rails.root.join("tmp/storage") %&amp;gt;

amazon:
  service: S3
  access_key_id: ""
  secret_access_key: ""
  bucket: ""
  region: "" # e.g. 'us-east-1'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You don't have to set this up for each environment, however, if you have multiple environments, it is likely that they will each store uploads in a different place so it's good to get this configured. Part of what makes Active Storage so great is that it comes with disk storage for your development and test environments. For production, depending on which service you use, you can configure that to be set up in this file (I chose Amazon S3 above, but have not filled in any of the credentials).&lt;/p&gt;

&lt;p&gt;With this in place, we then need to tell Active Storage which service to use and we'll do that on a per-environment basis, too.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;config/environments/development.rb&lt;/code&gt; let's add:&lt;br&gt;
&lt;code&gt;config.active_storage.service = :local&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which means we'll store files locally for our development environment.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;config/environments/test.rb&lt;/code&gt; let's add:&lt;br&gt;
&lt;code&gt;config.active_storage.service = :test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which means we'll temporarily store files locally (notice in the above &lt;code&gt;config/storage.yml&lt;/code&gt; file that we're storing to the &lt;code&gt;tmp/storage&lt;/code&gt; directory and not the &lt;code&gt;storage&lt;/code&gt; directory).&lt;/p&gt;

&lt;p&gt;And, lastly, in &lt;code&gt;config/environments/production.rb&lt;/code&gt; let's add:&lt;br&gt;
&lt;code&gt;config.active_storage.service = :amazon&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which means we'll store our files on Amazon.&lt;/p&gt;

&lt;p&gt;With all of this set up, we have two more major steps: Attaching the files to our Model and displaying that attached file to the user in our UI.&lt;/p&gt;

&lt;p&gt;To attach the file to our model: use the &lt;code&gt;has_one_attached&lt;/code&gt; relationship to set up a one-to-one mapping between the record and the files; or, use the &lt;code&gt;has_many_attached&lt;/code&gt; relationship to set up a many-to-one mapping between the record and the files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;has_one_attached&lt;/strong&gt; example:&lt;br&gt;
A user has one uploaded image associated with it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  has_one_attached :image
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;has_many_attached&lt;/strong&gt; example:&lt;br&gt;
A Blog Post has many uploaded images associated with it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Post &amp;lt; ApplicationRecord
  has_many_attached :images
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;NOTE: to see how to fully set up the &lt;code&gt;has_many_attached&lt;/code&gt; association see the &lt;a href=""&gt;docs&lt;/a&gt;. To keep things simple, I'll set up the UI using the &lt;code&gt;has_one_attached&lt;/code&gt; relationship.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finally, to allow users to upload the images in the UI and then display the image back to the user, we can add an upload form field to our page by adding this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;%= form.file_field :image %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add the &lt;code&gt;:image&lt;/code&gt; param into your params that you permit in the controller; for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def user_params
   params.require(:user).permit(:email, :password, :image)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With this in place, we can display the image back to a user (if there is one attached) so that they can see their attached file (in the &lt;code&gt;user#show&lt;/code&gt; part of the application):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;% if @user.image.attached? %&amp;gt;
   &amp;lt;img src="&amp;lt;%=(url_for(@user.image)) %&amp;gt;"&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;attached?&lt;/code&gt; is a helpful built-in method that tells us if the image for that user has been attached. You can also call &lt;code&gt;attach&lt;/code&gt; on an existing user to attach an image to that user by doing: &lt;code&gt;&lt;br&gt;
user.image.attach(params[:image])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And, there you have it! A way to add an image uploader to your rails application using the built-in Rails Active Storage 🎉&lt;/p&gt;

</description>
      <category>rails</category>
      <category>activestorage</category>
      <category>fileuploader</category>
    </item>
  </channel>
</rss>
