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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

2回答

1691閲覧

【Ruby on Rails】users/show.html.erbにユーザーが投稿した写真の一覧を出したい

is02

総合スコア17

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/05 05:31

編集2020/02/05 05:34

前提・実現したいこと

users/show.html.erbにユーザーが投稿した写真の一覧を出したい

発生している問題・エラーメッセージ

イメージ説明

該当のソースコード

####モデル
user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user # 2~8文字以内で名前が入っているかの確認 validates :name, presence: true, length: { in: 2..8 }, uniqueness: true # 自己紹介 100文字以内 validates :introduction, length: { maximum: 100 } # 可能なコスプレ 100文字以内 validates :like_cos, length: { maximum: 100 } # refile定義 attachment :profile_image # フォロー機能のメソッド def follow(other_user) unless self == other_user # フォローしようとしている人が自分自身ではないか # フォローしようとしている人が自分以外ならフォローする self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship # フォローしていたらアンフォローする end def following?(other_user) self.followings.include?(other_user) # other_userが含まれていたらtrueを返す end end

post_image.rb

class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy # refile定義 attachment :real_image attachment :cosplay_image validates :real_image, presence: true validates :cosplay_image, presence: true # 投稿を降順に並び替えし、最新のものを上にくるようにする default_scope -> { order(created_at: :asc) } end

####コントローラー
users_controller.rb

class UsersController < ApplicationController def show @user = User.find(params[:id]) @post_images = @user.post_images.map(&:cosplay_image) end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to user_path(@user.id) else render 'edit' end end def following @user = User.find(params[:id]) @users = @user.followings render 'show_follow' end def followers @user = User.find(params[:id]) @users = @user.followers render 'show_follower' end private def user_params params.require(:user).permit(:name, :profile_image, :introduction, :like_cos) end end

post_images_controller.rb

class PostImagesController < ApplicationController def new @post_image = PostImage.new end # 投稿データの保存 def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id if @post_image.save redirect_to post_images_path else render 'new' end end def index @post_images = PostImage.page(params[:page]).reverse_order end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private # 投稿データのストロングパラメータ def post_image_params params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count) end end

####ビュー
users/show.html.erb

<div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿一覧', root_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="mypage_wrapper"> <div class="mypage_innner_wrapper"> <div class="profile_image"> <%= attachment_image_tag @user, :profile_image, format: 'jpeg', fallback: "no_image.jpg" %> </div> <div class="profile_content"> <h1><%= @user.name %></h1> <% if @user.id == current_user.id %> <%= link_to "プロフィールを編集", edit_user_path(@user), class: "profile_edit_path" %> <% end %> </div> <div class="profile_count"> <ul> <li> <%= @post_images.count %> </li> <li> <%= link_to following_user_path(@user) do %> <%= @user.followings.count %> <% end %> </li> <li> <%= link_to followers_user_path(@user) do %> <%= @user.followers.count %> <% end %> </li> </ul> </div> <div class="profile_count_name"> <ul> <li class="first">投稿</li> <li>フォロー</li> <li>フォロワー</li> </ul> </div> <div class="introduction"> <h4>自己紹介</h4> <%= @user.introduction %> </div> <div class="like_cos"> <h4>実装可能なコスプレ</h4> <%= @user.like_cos %> </div> <div class="user_gallery"> <%= @post_images.each do |post_image| %> <%= attachment_image_tag post_image, :cosplay_image %> <% end %> </div> </div> </div>

補足情報(FW/ツールのバージョンなど)

ruby 2.5.7p206
Rails 5.2.4.1

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

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

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

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

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

guest

回答2

0

自己解決

users_controller showアクション内
@post_images = @user.post_imagesにして、

viewで
<% @post_images.each do |post_image| %>
<%= attachment_image_tag post_image, :cosplay_image %>
<% end %>
記載したところ直りました。

<% @post_images.each do |post_image| %>で、はじめのところに=がついていたので変な文字が表示されていました。

投稿2020/02/07 01:49

is02

総合スコア17

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

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

0

<%= attachment_image_tag post_image, :cosplay_image %> <% end %>

では post_image は class PostImag であることを期待しているようですが
@post_images = @user.post_images.map(&:cosplay_image)なので違っています。
@post_images = @user.post_images としてください

投稿2020/02/05 21:20

winterboum

総合スコア23284

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

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

is02

2020/02/06 08:32

回答ありがとうございます。 おっしゃられたように変更すると、cosplay_image以外のものも出てしまうんですが、cosplay_imageのみ表示させる方法などありますでしょうか。
winterboum

2020/02/06 08:42

<%= attachment_image_tag post_image, :cosplay_image %> この部分でですか?
is02

2020/02/06 08:45

はい、そうです。
winterboum

2020/02/06 08:55

以外のもの とは何か、具体的に書いてください。
is02

2020/02/06 09:04

[#<PostImage id: 3, real_image_name: "ふぇあふぇあfふぁえふぁ", real_image_id: "82e4af43678ed234a3632aa938f692635e7ed076f60c8090fa...", caption: "ふぇあふぇあfFate", user_id: 1, created_at: "2020-02-05 03:10:28", updated_at: "2020-02-05 03:10:28", cosplay_image_id: "b32033690a52b763d31fcdb7ccf294fb25dabdb106a541d57a...", cosplay_image_name: "ふぁえあf", favorites_count: 6, cosplay_favorites_count: 3>, #<PostImage id: 4, real_image_name: "faewaw", real_image_id: "94f0adf624ffade1b8b7c4d91253653dab762866d4c967781d...", caption: "fwfaewf", user_id: 1, created_at: "2020-02-06 08:54:07", updated_at: "2020-02-06 08:54:07", cosplay_image_id: "dcc60adb76d6542a5fefff06d00702819055a4be8d2fe5191b...", cosplay_image_name: "fewfewafewafwa", favorites_count: nil, cosplay_favorites_count: nil>] PostImageの id, real_image_name, real_image_id, caption, user_id, created_at, updated_at, cosplay_image_id, cosplay_image_name, favorites_count, cosplay_favorites_countが上記のような文字で表示されてしまいます。 また、2個投稿しているため、2個分でています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問