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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

2回答

2568閲覧

【Ruby on Rails】他のモデルにある画像を表示させたい

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/01/29 01:09

編集2020/01/29 02:15

前提・実現したいこと

#####post_imageに保存されているcosplay_imageを、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 } # 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 # 投稿を降順に並び替えし、最新のものを上にくるようにする 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 end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) @user.update(user_params) redirect_to user_path(@user.id) 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 @post_image.save redirect_to post_images_path 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

<%= attachment_image_tag @post_images, :cosplay_image %>

試したこと

・user.rbに「attachment :cosplay_image」を追加
・users_controllerのshowアクション内、
@post_images = @user.post_imagesを
@post_images = @user.post_images.cosplay_imageに変更

上記を試してみましたが、エラー内容変わらず。

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

ruby 2.5.7p206
Rails 5.2.4.1

追記

・users_controller内showアクションの記述を変更しました。
・users/show.html.erb内の記述を変更しました。

users_controller.rb

def show @user = User.find(params[:id]) @post_images = @user.post_images.map(&:cosplay_image) end

users/show.html.erb

<%= @post_images.each do |image| %> <%= attachment_image_tag image, :cosplay_image %> <% end %>

その際に下記のエラーが発生します。
イメージ説明

恐らく、
・Userモデルにそもそもcosplay_imageというカラムが存在しない
・user.rbにattachment :cosplay_imageの記載が無い
のではないかと考えていますが、解決方法が分かりません。
アドバイスを頂けないでしょうか。

下記は、usersとpost_imagesのschemaです。

users

create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "profile_image_id" t.string "like_cos" t.text "introduction" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end

post_images

create_table "post_images", force: :cascade do |t| t.text "real_image_name" t.string "real_image_id" t.text "caption" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "cosplay_image_id" t.text "cosplay_image_name" t.integer "favorites_count" t.integer "cosplay_favorites_count" end

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

クリップした質問は、後からいつでも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:50

is02

総合スコア17

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

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

0

post_imagesと複数形になっていますが、これはPostImageコレクションになっていますので、1モデル単位に適用するcosplay_imageのようなメソッドは直接使えません。

post_imagesが複数あった場合も表示させる画像は1枚でいいのか、それとも全部表示するのかによって、適切な実装が異なってきます。

投稿2020/01/29 01:17

maisumakun

総合スコア145183

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

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

is02

2020/01/29 01:24

回答ありがとうございます。 @userの持つcosplay_imageという画像は全て表示させたいと考えております。
maisumakun

2020/01/29 01:41

でしたら、簡便には「@cosplay_images = @user.post_images.map(&:cosplay_image)」として、ビューでは「@cosplay_images.each do |image|」から1つずつ表示していくような形となります。
is02

2020/01/29 02:18

教えて頂いた書き方で書いてみましたが、<%= attachment_image_tag image, :cosplay_image %>でエラーが発生します。Refileが定義されていない?のようなエラーが発生していると考え、user.rbにattachment :cosplay_imageと記述してみましたが、変わりませんでした。 お時間のある時にご返信いただけると幸いです。よろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問