前提・実現したいこと
#####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
回答2件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。