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

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

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

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1294閲覧

投稿したprototypeをcollectionを使って表示させたい

yozakura10

総合スコア8

Ruby

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/10/26 07:06

編集2020/10/26 07:46

前提・実現したいこと
現在の画面
このプロトタイプと書いてある下に投稿したのを一覧表示させたい
部分テンプレートは作ってあリマス

発生している問題・エラーメッセージ
投稿した数だけ表示させる方法がわからない

画面の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

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

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

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

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

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

guest

回答1

0

ベストアンサー

こちらの質問と同様に
UsersController#showならば

ruby

1class UsersController < ApplicationController 2 def show 3 @user = User.find(params[:id]) 4 @prototype = @user.prototypes 5 end 6end

となるでしょう。

なお@prototypeですと変数名として単数形なので
それをcollectionに渡し展開するのは個人的には違和感があります。

また、部分テンプレート内の

erb

1<%= link_to 'by プロトタイプの投稿者名', user_path(current_user), method: :get, class: :card__user %>

current_userを参照している事が疑問です。

投稿2020/10/26 08:13

asm

総合スコア15147

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

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

yozakura10

2020/10/27 05:55

解決済み後にすみません、current_userがおかしいとのことですが 通常であればどのように修正するのが良いのでしょうか
asm

2020/10/27 14:31

prototype.userですね
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問