前提・実現したいこと
現在の画面
このプロトタイプと書いてある下に投稿したのを一覧表示させたい
部分テンプレートは作ってあリマス
発生している問題・エラーメッセージ
投稿した数だけ表示させる方法がわからない
画面のhtml
html
1<div class="main"> 2 <div class="inner"> 3 <div class="user__wrapper"> 4 <h2 class="page-heading"> 5 <%= @user.name + "さんの情報" %> 6 </h2> 7 <table class="table"> 8 <tbody> 9 <tr> 10 <th class="table__col1">名前</th> 11 <td class="table__col2"><%= @user.name %></td> 12 </tr> 13 <tr> 14 <th class="table__col1">プロフィール</th> 15 <td class="table__col2"><%= @user.profile %></td> 16 </tr> 17 <tr> 18 <th class="table__col1">所属</th> 19 <td class="table__col2"><%= @user.occupation %></td> 20 </tr> 21 <tr> 22 <th class="table__col1">役職</th> 23 <td class="table__col2"><%= @user.position %></td> 24 </tr> 25 </tbody> 26 </table> 27 <h2 class="page-heading"> 28 <%= @user.name + "さんのプロトタイプ" %> 29 </h2> 30 <div class="user__card"> 31 <%# 部分テンプレートでそのユーザーが投稿したプロトタイプ投稿一覧を表示する %> 32 <%= render partial: "prototypes/prototype", collection: @prototype %> 33 </div> 34 </div> 35 </div> 36</div>
部分テンプレートのhtml
html
1<div class="card"> 2 <%= image_tag prototype.image, class: 'card__img' if prototype.image.attached? %> 3 <div class="card__body"> 4 <%= link_to 'プロトタイプのタイトル', prototype_path(prototype.id), method: :get, class: :card__title%> 5 <p class="card__summary"> 6 <%= "プロトタイプのキャッチコピー" %> 7 </p> 8 <%= link_to 'by プロトタイプの投稿者名', user_path(current_user), method: :get, class: :card__user %> 9 </div> 10</div>
prototypes_controllerです
ruby
1class PrototypesController < ApplicationController 2 3 def index 4 @prototypes = Prototype.all 5 end 6 7 def new 8 @prototype = Prototype.new 9 end 10 11 def create 12 @prototype = Prototype.new(prototype_params) 13 if @prototype.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 def show 21 @prototype = Prototype.find(params[:id]) 22 @comment = Comment.new 23 @comments = @prototype.comments.includes(:user) 24 25 end 26 27 def edit 28 @prototype = Prototype.find(params[:id]) 29 end 30 31 def update 32 prototype = Prototype.find(params[:id]) 33 if prototype.update(prototype_params) 34 redirect_to prototype_path 35 else 36 render :edit 37 end 38 end 39 40 def destroy 41 prototype = Prototype.find(params[:id]) 42 prototype.destroy 43 redirect_to root_path 44 end 45 46 private 47 def prototype_params 48 params.require(:prototype).permit(:title, :catct_copy, :concept, :image).merge(user_id: current_user.id) 49 end 50 51end
users_controllerです
ruby
1class UsersController < ApplicationController 2 before_action :move_to_index, except: [:index, :show] 3 4 5 def index 6 7 end 8 9 def new 10 @user = User.new 11 end 12 13 def edit 14 end 15 16 def show 17 @user = User.find(params[:id]) 18 end 19 20 21 def move_to_index 22 unless user_signed_in? 23 redirect_to action: :index 24 end 25 end 26 27 28 # private 29 # def user_params 30 # params.require(:user).permit(:name, :profile, :occupation, :position).merge(user_id: current_user.id) 31 # end 32 33end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/27 05:55
2020/10/27 14:31