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

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

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

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

Ruby on Rails

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

HTML

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

Q&A

解決済

1回答

5305閲覧

各ユーザーの詳細ページに行きたい

yozakura10

総合スコア8

Ruby

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

Ruby on Rails

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

HTML

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

0グッド

1クリップ

投稿2020/10/27 09:40

編集2020/10/27 12:21

1前提・実現したいこと
ユーザー名を押したとき各ユーザーの詳細ページに行きたい
現在はログインしているユーザーのところにしかいけない
それぞれのユーザーのページに行きたい
現在の状況
現在の画面
アプリのurl

該当コード
app/views/prototypes/index.html.erb

HTML

1<main class="main"> 2 <div class="inner"> 3 <%# ログインしているときは以下を表示する %> 4 <% if user_signed_in? %> 5 <div class="greeting"> 6 <%= "こんにちは" %> 7 <%#= link_to "#{current_user.name}", user_path(@prototype.user), class: :greeting__link %> 8 <%= link_to "#{current_user.name}", user_path(current_user), class: :greeting__link %> 9 <%# <%= link_to "#{current_user.name}", user_registration_path, class: :greeting__link %> 10 </div> 11 <% else %> 12 <div class="grid-6"> 13 <%= link_to "", new_user_session_path, class: "post" %> 14 <%= link_to "", new_user_registration_path, class: "post" %> 15 </div> 16 <% end %> 17 </div> 18 <%# // ログインしているときは上記を表示する %> 19 <div class="card__wrapper"> 20 <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> 21 <%= render partial: "prototype", collection: @prototypes %> 22 </div> 23 </div> 24</main> 25 26``` 27 28app/views/prototypes/_prototype.html.erb 29```html 30<div class="card"> 31 <%= link_to prototype_path(prototype.id),method: :get do %> 32 <%= image_tag prototype.image, class: 'card__img' if prototype.image.attached? %> 33 <% end %> 34 <div class="card__body"> 35 <%= link_to prototype.catct_copy, prototype_path(prototype.id), method: :get, class: :card__title%> 36 <p class="card__summary"> 37 <%= link_to prototype_path(prototype.id),method: :get do %> 38 <%= prototype.concept %> 39 <% end %> 40 </p> 41 <% if current_user %> 42 <%#= link_to "by #{prototype.user.name}", user_path(current_user, method: :get, class: :card__user) %> 43 <%= link_to "by #{prototype.user.name}", user_path(current_user, method: :get, class: :card__user) %> 44 <% end %> 45 </div> 46</div> 47 48``` 49 50app/views/prototypes/show.html.erb 51```html 52<main class="main"> 53 <div class="inner"> 54 <div class="prototype__wrapper"> 55 <p class="prototype__hedding"> 56 <%= @prototype.title%> 57 </p> 58 59 <%# <%= @comment.text %> 60 <%= link_to "by #{@prototype.user.name}さん" , user_path(@prototype.user), class: :prototype__user %> 61 <%#= link_to "by #{@prototype.user.name}さん" , "/users/#{@comment.user_id}" , class: :prototype__user %> 62 <%# <%= link_to "by #{@prototype.user.name}さん" , user_path(current_user), class: :prototype__user %> 63 <%# 上の記述で@prototypeにuserモデルからnameカラムを持ってくるコードができた %> 64 <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> 65 <% if current_user == @prototype.user%> 66 <div class="prototype__manage"> 67 <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %> 68 <%= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %> 69 </div> 70 <% end %> 71 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 72 <div class="prototype__image"> 73 <%= image_tag @prototype.image %> 74 </div> 75 <div class="prototype__body"> 76 <div class="prototype__detail"> 77 <p class="detail__title">キャッチコピー</p> 78 <p class="detail__message"> 79 <%= @prototype.catct_copy%> 80 </p> 81 </div> 82 <div class="prototype__detail"> 83 <p class="detail__title">コンセプト</p> 84 <p class="detail__message"> 85 <%= @prototype.concept%> 86 </p> 87 </div> 88 </div> 89 <div class="prototype__comments"> 90 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 91 <% if current_user %> 92 <%= form_with model: [@prototype, @comment], local: true do |f|%> 93 <%# (model: [@prototype, @comment], local: true)にするといいかも %> 94 <div class="field"> 95 <%= f.label :text, "コメント" %><br /> 96 <%= f.text_field :text %> 97 </div> 98 <div class="actions"> 99 <%= f.submit "送信する", class: :form__btn %> 100 </div> 101 <% end %>   102 <% end %> 103 <%# // ログインしているユーザーには上記を表示する %> 104 <ul class="comments_lists"> 105 <%# 投稿に紐づくコメントを一覧する処理を記述する %> 106 <% if @comments %> 107 <% @comments.each do |comment| %> 108 <li class="comments_list"> 109 <%#= link_to comment.user.name, user_path(current_user), class: :comment_user %> 110 <%= link_to comment.user.name, "/users/#{comment.user_id}", class: :comment_user %> 111 <%= comment.text %> 112 <% end %> 113 <% end %> 114 </li> 115 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 116 </ul> 117 </div> 118 </div> 119 </div> 120</main> 121 122``` 123 124 125config/routes.rb 126```ruby 127Rails.application.routes.draw do 128 devise_for :users 129 root to: "prototypes#index" 130 resources :users, only: [:new, :edit, :update, :show] 131 resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy,] do 132 resources :comments, only: [:create] 133 end 134 135 devise_scope :user do 136 get '/users/sign_out' => 'devise/sessions#destroy' 137 end 138 139end 140 141 142``` 143 144app/controllers/users_controller.rb 145```ruby 146 147class UsersController < ApplicationController 148 before_action :move_to_index, except: [:index, :show] 149 150 151 def index 152 153 end 154 155 def new 156 @user = User.new 157 end 158 159 def edit 160 end 161 162 def show 163 @user = User.find(params[:id]) 164 @prototypes = @user.prototypes 165 end 166 167 168 def move_to_index 169 unless user_signed_in? 170 redirect_to action: :index 171 end 172 end 173 174 175 # private 176 # def user_params 177 # params.require(:user).permit(:name, :profile, :occupation, :position).merge(user_id: current_user.id) 178 # end 179 180end 181``` 182 183**やったこと** 184indexと_prototypeのuser_path(current_user)の部分を 185user_path(@prototype.user)にしてみましたが_以下のエラーが出ました 186 187 188 189``` 190NoMethodError in Users#show 191Showing /Users/kamiyaryota/Desktop/projects/protospace-31035/app/views/prototypes/_prototype.html.erb where line #14 raised: 192 193undefined method `id' for nil:NilClass 194Extracted source (around line #14): 19512 19613 19714 19815 19916 20017 201 202 <% if current_user %> 203 <%#= link_to "by #{prototype.user.name}", user_path(current_user, method: :get, class: :card__user) %> 204 <%= link_to "by #{prototype.user.name}", user_path(@prototype.id), class: :card__user %> 205 <%#= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %> 206 <% end %> 207 </div> 208 209Trace of template inclusion: #<ActionView::Template app/views/users/show.html.erb locals=[]> 210 211Rails.root: /Users/kamiyaryota/Desktop/projects/protospace-31035 212 213Application Trace | Framework Trace | Full Trace 214app/views/prototypes/_prototype.html.erb:14 215app/views/users/show.html.erb:34 216Request 217Parameters: 218 219{"id"=>"2"} 220``` 221というエラーが出てしまいます 222なのでどこのファイルのどの部分を直したらいいのかも詳細に教えてもらえたら助かります

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

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

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

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

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

guest

回答1

0

ベストアンサー

そりゃそうでしょう。indexもpartialもcurrent_userかそのprototypesしか載せていないのですから。
prototypes/index.html.erb の中身は prototypes/show.html.erb にみえますし。

どの画面の「ユーザー名」を押すのでしょう???

投稿2020/10/27 11:39

winterboum

総合スコア23347

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

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

yozakura10

2020/10/27 12:14

画面はindex.htmlの8行目の部分と_prototype.html.erbの14行目の部分です。 画面のボタンでリンクにあるby testや by aaaaと書いてあるところです。 indexの記載はこれですのでshowのコードも追加しておきました。 current_userの部分を@prototype.userに変えたのですがshow.htmlだとこれでうまくできるのですが上記2つのファイルでやるとエラーが出ます。 @prototype.userはuser.contollerで定義しているのですが。似たような質問を何度もしてしまいすみませんが答えていただきますと助かります
yozakura10

2020/10/27 12:18

画像を追加しました、画像の右下あたりにあるやつです。わかりにくいのであれば画面を変えます
winterboum

2020/10/27 12:48

ですからそれらが「current_userかそのprototypes」ですね? index は 「目次」ですから、通常は一覧を載せます。 一覧なら色々なユーザの情報が載りますから、そこからなら行けます。 まず indexを一覧になるようにしてください。
yozakura10

2020/10/28 06:25

app/views/prototypes/_prototype.html.erbのcurrent_userをprototype.userにしたらいけました indexの変数をみたら見つけることができました 教えていただきありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問