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

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

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

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

Q&A

解決済

1回答

1994閲覧

投稿一覧画面にユーザーのプロフィール画像を表示させたい

ckr

総合スコア23

Ruby on Rails 5

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

0グッド

0クリップ

投稿2020/04/22 14:53

投稿一覧画面にユーザーのプロフィール画像を表示させたいのですが、
<%= image_tag post.user.profile_photo %>と記載するとエラーが出てしまい
プロフィール画像が表示されない状況です。
イメージ説明

posts/index.html.erb(一旦上記ソースコードはコメントアウトしております)

1 2<%= search_form_for @search, url: posts_path do |f| %> 3<div class="form-group"> 4 <%= f.label :title_or_contents_cont, 'エージェントを検索しよう' %> 5 <%= f.search_field :title_or_contents_cont,class:"form-control"%> 6 </div> 7 <div class="actions"><%= f.submit '検索' %></div> 8<% end %> 9 10 <div class="row"> 11 <% @posts.each do |post| %> 12 <div class="col-md-4"> 13 14 <div class="card"%> 15 16 <div class="card-body text-center"> 17 <h4 class="card-title"><%=post.title %></h4> 18 <p class="card-text"><%=post.contents %></p> 19 20 <%#= image_tag post.user.profile_photo %> 21 <p><%= image_tag post.user.profile_photo unless post.user.profile_photo..nil? %></p> 22 23 <strong><%= post.user.name %></strong> 24 25 <a href="#" class="btn btn-primary">ゴシメイする</a> 26 </div> 27 28 </div> 29 </div> 30 <%end%> 31 32

posts.controller

1class PostsController < ApplicationController 2 def new 3 @post = Post.new 4 #@post.user.profile_photo 5 end 6 7 def create 8 @post = Post.new(post_params) 9 if @post.save 10 redirect_to posts_path 11 flash[:alert] = "ボシュウ開始されました!" 12 else 13 redirect_to new_post_path 14 flash[:alert] = "投稿に失敗しました" 15 end 16 end 17 18 def index 19 @search = Post.ransack(params[:q]) 20 @posts = @search.result 21 #user = @post.user 22 # @posts = Post.all.order('created_at DESC') 23 #@user.profile_photo = User.profile_photo 24 25 end 26 27 def edit 28 end 29 30 def show 31 @post = Post.find_by(id: params[:id]) 32 end 33 34 35 36 private 37 def post_params 38 params.require(:post).permit(:contents,:title).merge(user_id: current_user.id) 39 end 40end

post.rb

1class Post < ApplicationRecord 2 validates :title, presence: true,uniqueness: { scope: :user_id } 3 validates :contents, presence: true,uniqueness: { scope: :user_id } 4 belongs_to :user 5end 6

お知恵をお借りできますと幸いです。
何卒宜しくお願い致します。

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

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

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

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

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

guest

回答1

0

自己解決

こちらの質問ですが、プロフィール画像をアップすることができましたので
念のため、解決方法を記載します。

★状況
companyモデルが投稿したpostを表示する際に、companyモデルのprofile_photoを
postと一緒に表示させたい。

修正後の、posts/index.html.erb <div class="form-group"> <%= f.label :title_or_contents_cont, 'エージェントを検索しよう' %> <%= f.search_field :title_or_contents_cont,class:"form-control"%> </div> <div class="actions"><%= f.submit '検索' %></div> <% end %> <div class="row"> <% @posts.each do |post| %> <div class="col-md-6"> <div class="card"%> <div class="card-body text-center"> <h4 class="card-title"><%=post.title %></h4> <p class="card-text"><%=post.contents %></p> <%= image_tag post.company.profile_photo_url(:thumb),class: "round-img" %>

修正を加えた部分としては、
<%#= image_tag post.company.profile_photo unless post.company.profile_photo..nil? %>

を、
<%= image_tag post.company.profile_photo_url(:thumb),class: "round-img" %>
に変更しました。

投稿2020/05/03 04:58

ckr

総合スコア23

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問