1前提・実現したいこと
ユーザーのマイページ(app/views/users/show.html.erb)に
部分テンプレート(app/views/prototypes/_prototype.html.erb)を使って
ユーザーが投稿した写真を一覧で表示させたいが
表示されない。
2発生している問題・エラーメッセージ
<%= render partial: 'prototypes/prototype', collection: @prototypes %> を使って部分テンプレートを使うが、表示されなく、 エラーコードも表示されない 理想の状態 https://gyazo.com/841c79ee5480543857955c277fb5ea72 現状の状態 https://gyazo.com/ffa995eee01eb19c28ae77144bfd3d0c
3該当のソースコード
ユーザーのマイページ(app/views/users/show.html.erb)
<div class="main"> <div class="inner"> <div class="user__wrapper"> <h2 class="page-heading"> <%= "#{@user.name}さんの情報"%> </h2> <table class="table"> <tbody> <tr> <th class="table__col1">名前</th> <td class="table__col2"><%=@user.name %></td> </tr> <tr> <th class="table__col1">プロフィール</th> <td class="table__col2"><%= @user.profile %></td> </tr> <tr> <th class="table__col1">所属</th> <td class="table__col2"><%=@user.occupation %></td> </tr> <tr> <th class="table__col1">役職</th> <td class="table__col2"><%=@user.position %></td> </tr> </tbody> </table> <h2 class="page-heading"> <%= "#{@user.name}さんのプロトタイプ"%> </h2> <div class="user__card"> <%= render partial: 'prototypes/prototype', collection: @prototypes %>←**ここの部分** </div> </div> </div> </div>
部分テンプレート(app/views/prototypes/_prototype.html.erb)
<div class="card"> <%= link_to prototype_path(prototype.id),method: :get do %> <%= image_tag prototype.image.variant(resize: '300x300'), class: :card__img if prototype.image.attached? %> <% end %> <div class="card__body"> <%= link_to prototype.title, prototype_path(prototype.id),method: :get, class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by#{prototype.user.name}", user_path(current_user), class: :card__user %> %> </div> </div>
prototypes_controller.erb
class PrototypesController < ApplicationController before_action :authenticate_user!, only: [:new,] def index @prototypes = Prototype.all end def show @prototype = Prototype.find(params[:id]) @comment = Comment.new @comments = Comment.all end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path else render :new end end def edit @prototype = Prototype.find(params[:id]) if @prototype.user_id == current_user.id render :edit else redirect_to root_path end end def update @prototype = Prototype.find(params[:id]) if @prototype.update(prototype_params) redirect_to prototype_path else render:edit end end def destroy @prototype = Prototype.find(params[:id]) if @prototype.destroy redirect_to root_path end end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept,:image).merge(user_id: current_user.id) end end
users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) end end
4自分で調べたことや試したこと
アソシエーションを確認
userモデル
has_many :prototypes
has_many :comments
prototypeモデル
belongs_to :user
has_many :comments,dependent: :destroy
commentモデル
belongs_to :prototype
belongs_to :user
の記述は確認できた
またapp/views/users/のディレクトリに
_prototype.html.erbをコピーして入れてみたが
表示はされなかった
5使っているツールのバージョンなど補足情報

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/25 23:07
2020/10/26 00:12
2020/10/26 01:56
2020/10/26 02:23
2020/10/26 05:09
2020/10/26 05:35