Preamble
When storing files in Rails, the first toolkit that we approach is 3rd party gems such as CarrierWave or Paperclip (before not being used as a replacement for Active Storage). Active Storage was introduced with Rails 5.2.
Active Storage facilitates the upload of files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage, and attaches those files to Active Record objects. It comes with a local disk-based service for development and testing, and supports file replication to subordinate services for backup and migration.
Using Active Storage, an application can convert image uploads with ImageMagick, create image presentations of non-image uploads like PDFs and videos, and extract metadata from arbitrary file.
Perform
Go to the terminal and create a new rails application
rails new app
Create an application, create a simple blog app with the scaffold command.
rails g scaffold Post title: string content: text
After the process is completed, run the migrate
rails db: migrate
Then enter custom routes
1 2 3 4 5 |
<span class="token constant">Rails</span> <span class="token punctuation">.</span> application <span class="token punctuation">.</span> routes <span class="token punctuation">.</span> draw <span class="token keyword">do</span> resources <span class="token symbol">:posts</span> root to <span class="token punctuation">:</span> <span class="token string">'posts#index'</span> <span class="token keyword">end</span> |
Now we will add an image to our post. First we need to install active storage on our posts.
rails active_storage: install
After running this command, Rails will generate a migration file for you. When testing, you will see two different tables: active_storage_blobs and active_storage_attachments.
Then we run the migrate file
rails db: migrate
Next we add the following model:
1 2 3 4 |
<span class="token keyword">class</span> <span class="token class-name">Post</span> <span class="token operator"><</span> <span class="token constant">ApplicationRecord</span> has_one_attached <span class="token symbol">:image</span> <span class="token keyword">end</span> |
Again, we will need to add the image to our controller at the end of the post_params method, adding the image to the modle properties.
1 2 3 4 |
<span class="token keyword">def</span> <span class="token method-definition"><span class="token function">post_params</span></span> params <span class="token punctuation">.</span> <span class="token keyword">require</span> <span class="token punctuation">(</span> <span class="token symbol">:post</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> permit <span class="token punctuation">(</span> <span class="token symbol">:title</span> <span class="token punctuation">,</span> <span class="token symbol">:content</span> <span class="token punctuation">,</span> <span class="token symbol">:image</span> <span class="token punctuation">)</span> <span class="token keyword">end</span> |
In app / view / posts / _form.html.erb, we will edit the file by adding input to the image.
1 2 3 4 5 |
<span class="token operator"><</span> div <span class="token keyword">class</span> <span class="token operator">=</span> <span class="token string">"field"</span> <span class="token operator">></span> <span class="token operator"><</span> <span class="token string">%= form.label :image %> <%=</span> form <span class="token punctuation">.</span> file_field <span class="token symbol">:image</span> <span class="token string">%> </div></span> |
Now we need to load the images in the index and show
1 2 3 4 5 6 |
<span class="token comment">#app/views/posts/index.html.erb</span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token keyword">if</span> post <span class="token punctuation">.</span> image <span class="token punctuation">.</span> attached <span class="token operator">?</span> <span class="token string">%> <td></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token operator">=</span> image_tag post <span class="token punctuation">.</span> image <span class="token string">%></td></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token keyword">end</span> <span class="token operator">%</span> <span class="token operator">></span> |
1 2 3 4 5 6 7 8 |
<span class="token comment">#app/views/posts/show.html.erb</span> <span class="token operator"><</span> p <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token keyword">if</span> <span class="token variable">@post</span> <span class="token punctuation">.</span> image <span class="token punctuation">.</span> attached <span class="token operator">?</span> <span class="token string">%> <%= image_tag @post.image %></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token keyword">end</span> <span class="token string">%> </p></span> |
Now it is possible to post images in a post, but now we want to use the gem active_storage_validations when we need to post an image. We add the gem “active_storage_validations” into the gem file and bundle install. Later :
1 2 3 4 |
<span class="token comment"># app/models/post.rb</span> validates <span class="token symbol">:image</span> <span class="token punctuation">,</span> attached <span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">,</span> content_type <span class="token punctuation">:</span> <span class="token string">%i[png jpg jpeg]</span> |
With the above validation we only accept images with png, jpg and jpeg extensions attached to it when creating a post must have photos attached when creating a post. So we can post with the image file attached.
Reference source:
https://hackernoon.com/uploading-files-into-ruby-on-rails-activestorage-gv1r3ukr