質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Q&A

0回答

807閲覧

attachment_image_tagが表示されない

tomomomo

総合スコア22

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

0グッド

0クリップ

投稿2021/10/06 08:25

実装したいこと

TwitterのようなSNSアプリを作っており、フォロー機能を実装しております。
そこで、フォロー、フォロワーの一覧を表示させるにあたって該当のユーザーのアイコンを表示させたいと考えております。
post#indexに表示させようと考えております。

問題の内容

each文の中で各ユーザー名の名前を出し、アイコンも出そうとしたところエラーが出ました。
表題にもある通り、attachment_image_tagを使用しているのですが、(gemはrefileです)
表示されないので困っています。(検証ツールでもなにも出ませんでした。)
引数の与え方が違っているとは思うのですが、follow機能でrelation等少し複雑でわからない状況です。

該当コード、ファイル

以下に該当コードを載せます。
フォロー機能はrelationをユーザーモデルに記述しております。

UserModel

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable, :validatable 7 8 has_many :posts 9 has_many :post_images, dependent: :destroy 10 attachment :profile_image 11 12 has_many :follower, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy # フォロー取得 13 has_many :followed, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy # フォロワー取得 14 has_many :following_user, through: :follower, source: :followed # 自分がフォローしている人 15 has_many :follower_user, through: :followed, source: :follower # 自分をフォローしている人 16 17 def follow(user_id) 18 follower.create(followed_id: user_id) 19 end 20 21 def unfollow(user_id) 22 follower.find_by(followed_id: user_id).destroy 23 end 24 25 def following?(user) 26 following_user.include?(user) 27 end 28end

PostController

1def index 2 @post = Post.new 3 @posts = Post.all 4 @following_users = current_user.following_user 5 @follower_users = current_user.follower_user 6 end

PostIndexHTML

1<% @following_users.each do |following_user| %> 2 <div class="messages "> 3 <div class="message-box "> 4 ここが問題の箇所です!<%= attachment_image_tag @following_user, :profile_image, format: 'jpeg', size: "100x100" %> 5 <div class="message-content "> 6 <div class="message-header "> 7 フォロー中の名前は表示されています!<div class="name "><%= following_user.name %></div> 8 <div class="star-checkbox "> 9 <input type="checkbox " id="star-1 "> 10 <label for="star-1 "> 11 <svg xmlns="http://www.w3.org/2000/svg " width="20 " height="20 " viewBox="0 0 24 24 " fill="none " stroke="currentColor " stroke-width="2 " stroke-linecap="round " stroke-linejoin="round " class="feather feather-star "> 12 <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2 " /></svg> 13 </label> 14 </div> 15 </div> 16 <p class="message-line "> 17 I got your first assignment. It was quite good. ???? We can continue with the next assignment. 18 </p> 19 <p class="message-line time "> 20 Dec, 12 21 </p> 22 </div> 23 </div> 24 </div> 25 <% end %>

HTMLファイルのクラス名がおかしいのは気にしないでください。

UserMigration

1create_table "users", force: :cascade do |t| 2 t.string "email", default: "", null: false 3 t.string "encrypted_password", default: "", null: false 4 t.string "reset_password_token" 5 t.datetime "reset_password_sent_at" 6 t.datetime "remember_created_at" 7 t.string "name" 8 t.text "introduction" 9 t.datetime "deleted_at" 10 t.datetime "created_at", null: false 11 t.datetime "updated_at", null: false 12 t.string "profile_image_id" 13 t.index ["email"], name: "index_users_on_email", unique: true 14 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 15 end

以上、よろしくお願いします。
足りないもの等あればおっしゃっていただければと思います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問