1前提・実現したいこと
herokuを起動してエラーの確認をしたところTemplate::Errorが
出ており、そのエラーを解決したい
2発生している問題・エラーメッセージ
2020-10-26T10:37:33.229174+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] ActionView::Template::Error (No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id]): 2020-10-26T10:37:33.229175+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 5: <p class="card__summary"> 2020-10-26T10:37:33.229179+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 6: <%= "プロトタイプのキャッチコピー" %> 2020-10-26T10:37:33.229180+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 7: </p> 2020-10-26T10:37:33.229181+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 8: <%= link_to "by#{prototype.user.name}", user_path(current_user), method: :get, class: :card__user %> 2020-10-26T10:37:33.229181+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 9: </div> 2020-10-26T10:37:33.229181+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 10: </div> 2020-10-26T10:37:33.229182+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] 2020-10-26T10:37:33.229182+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] app/views/prototypes/_prototype.html.erb:8 2020-10-26T10:37:33.229183+00:00 app[web.1]: [f8aaa874-2dde-4c7e-a074-c59727357073] app/views/prototypes/index.html.erb:20
3該当のソースコード
app/views/prototypes/index.html.erb
html
1 2<main class="main"> 3 <div class="inner"> 4 <%# ログインしているときは以下を表示する %> 5 <% if user_signed_in? %> 6 <div class="greeting"> 7 <%= "こんにちは" %> 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>
app/views/prototypes/_prototype.html.erb
html
1 2<div class="card"> 3 <%= link_to prototype_path(prototype.id),method: :get do %> 4 <%= image_tag prototype.image, class: 'card__img' if prototype.image.attached? %> 5 <% end %> 6 <div class="card__body"> 7 <%= link_to prototype.catct_copy, prototype_path(prototype.id), method: :get, class: :card__title%> 8 <p class="card__summary"> 9 <%= link_to prototype_path(prototype.id),method: :get do %> 10 <%= prototype.concept %> 11 <% end %> 12 </p> 13 <% if current_user %> 14 <%= link_to "by#{prototype.user.name}", user_path(current_user, method: :get, class: :card__user) %> 15 <% end %> 16 </div> 17</div> 18
app/controllers/users_controller.rb
ruby
1 2class UsersController < ApplicationController 3 before_action :move_to_index, except: [:index, :show] 4 5 6 def index 7 8 end 9 10 def new 11 @user = User.new 12 end 13 14 def edit 15 end 16 17 def show 18 @user = User.find(params[:id]) 19 @prototypes = @user.prototypes 20 end 21 22 23 def move_to_index 24 unless user_signed_in? 25 redirect_to action: :index 26 end 27 end 28 29 30 # private 31 # def user_params 32 # params.require(:user).permit(:name, :profile, :occupation, :position).merge(user_id: current_user.id) 33 # end 34 35end
app/controllers/prototypes_controller.rb
ruby
1 2class PrototypesController < ApplicationController 3 before_action :authenticate_user!, except: [:index, :show] 4 5 def index 6 @prototypes = Prototype.all 7 end 8 9 def new 10 @prototype = Prototype.new 11 end 12 13 def create 14 @prototype = Prototype.new(prototype_params) 15 if @prototype.save 16 redirect_to root_path 17 else 18 render :new 19 end 20 end 21 22 def show 23 @prototype = Prototype.find(params[:id]) 24 @comment = Comment.new 25 @comments = @prototype.comments 26 27 end 28 29 def edit 30 @prototype = Prototype.find(params[:id]) 31 unless current_user == @prototype.user 32 redirect_to root_path 33 end 34 end 35 36 def update 37 @prototype = Prototype.find(params[:id]) 38 if @prototype.update(prototype_params) 39 redirect_to prototype_path(@prototype) 40 else 41 render :edit 42 end 43 end 44 45 def destroy 46 prototype = Prototype.find(params[:id]) 47 prototype.destroy 48 redirect_to root_path 49 end 50 51 52 private 53 def prototype_params 54 params.require(:prototype).permit(:title, :catct_copy, :concept, :image).merge(user_id: current_user.id) 55 end 56 57 58end
4、自分でしたこと
user_path(current_user)のcurrent_userが怪しいと考え
@prototypesなどに変えたがそうすると違ったメソッドエラーが出てしまいます
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。