[RUBY] [Rails] Show avatars in posts using Active Storage

Thing you want to do

I want to display avatars and user names in Posts # index (hereinafter referred to as timeline)

Premise

Introducing devise Introduced Active Storage Add avatar to users model

manner

Added has_one_attached and user instance methods for: avatar to the posts model

post.rb


class Post < ApplicationRecord
  validates :content, {presence: true, length: {maximum: 140}}
  validates :user_id, {presence: true}
    
  has_one_attached :avatar
  
  def user
    return User.find_by(id: self.user_id)
  end
end

Add .user to view

Ruby:index.html.erb


    <% @posts.each do |post| %>
    <div class="posts-index-item mb-20">
      <div class="posts-index-user">
        <!--avatar-->
        <div class="posts-index-img d-inline">
          <% if post.user.avatar.attached? %>
            <%= image_tag post.user.avatar, class: "avatar-index rounded-circle mx-auto" %>
          <% else %>
            <img class="avatar-index rounded-circle mx-auto" src="<%= "/images/default_user.png " %>" alt="Userimage">
          <% end %>
        </div>
        <!--username-->
        <div class="posts-index-username d-inline">
          <%= link_to post.user.username, users_show_path %>
        </div>
      </div>
      <!--content-->
      <%= link_to(post.content, "/posts/#{post.id}") %>
    </div>
  <% end %>

result

image.png

Summary

User model can be handled even in posts # index where params cannot be used by user method. ⇒ You can handle : avatar linked to the User model by post.user.avatar

Recommended Posts

[Rails] Show avatars in posts using Active Storage
[rails6.0.0] How to save images using Active Storage in wizard format
How to implement image posting function using Active Storage in Ruby on Rails
[Rails 6] Add images to seed files (using Active Storage)
Japaneseize using i18n with Rails
Create a drag-and-drop markdown editor in Rails 6 (using Active Storage, SimpleMDE and Inline Attachment)
[Rails API + Vue] Upload and display images using Active Storage
[Rails] How to use Active Storage
Rails Active Storage shrinks images before uploading
Show Better Errors in Rails + Docker environment
Implement button transitions using link_to in Rails
Dealing with NameError: uninitialized constant :: Analyzable error when installing Active Storage in Rails6
Create authentication function in Rails application using devise
Implement share button in Rails 6 without using Gem
[Rails] Run LINEBot in local environment using ngrok
[Rails] Show multi-level categories in the breadcrumb trail
Implement star rating function using Raty in Rails6
How to link images using FactoryBot Active Storage
[Rails] Implementation of image slide show using Bootstrap 3
[Rails] ActiveRecord :: HasManyThrough Order Error in Users # show
[Rails] Display popular posts in ranking format [Easy]
Introduce Active Storage
Group_by in Rails
docker + rails + nginx + webpacker + ssl + action cable + active storage
Arrange posts in order of likes on Rails (ranking)
[Ruby on Rails] I want to get the URL of the image saved in Active Storage