エラー内容にて「NameError in Books#show」と表示されるのですが、原因が分かりません。
下記にてコードを貼り付けておりますので、原因につきましてご教示いただけませんでしょうか。
よろしくお願いいたします。
ErrorText
1NameError in Books#show 2Showing /home/ec2-user/environment/bookers2-debug/app/views/users/_profile.html.erb where line #2 raised: 3 4undefined local variable or method `user' for #<#<Class:0x00007f171dae2870>:0x00007f171dc3bd70> 5Did you mean? super 6Extracted source (around line #2): 71 82 93 104 115 126 13 14<table class="table"> 15 ↓エラー該当箇所です 16 <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no_image.jpg" %></tr> 17 18 <tr> 19 <th>name</th> 20 <th><%= user.name %></th>
Controller
1class UsersController < ApplicationController 2 before_action :ensure_correct_user, only: [:update] 3 4 def index 5 @users = User.all 6 @book = Book.new 7 end 8 9 def show 10 @user = User.find(params[:id]) 11 @books = @user.books 12 @book = Book.new 13 end 14 15 def edit 16 @user = User.find(params[:id]) 17 end 18 19 def update 20 if @user.update(user_params) 21 redirect_to users_path(@user), notice: "You have updated user successfully." 22 else 23 render :show 24 end 25 end 26 27 private 28 def user_params 29 params.require(:user).permit(:name, :introduction, :profile_image) 30 end 31 32 def ensure_correct_user 33 @user = User.find(params[:id]) 34 unless @user == current_user 35 redirect_to user_path(current_user) 36 end 37 end 38end 39
showhtmlerb
1<div class="row"> 2 <div class="col-xs-3"> 3 <h2>User info</h2> 4 <%= render 'profile', user: @user %> 5 6 <h2>New book</h2> 7 <%= render 'books/form', book: @book %> 8 </div> 9 10 <div class="col-xs-9"> 11 <h2>Books</h2> 12 <table class="table table-hover table-inverse"> 13 <thead> 14 <tr> 15 <th></th> 16 <th>Title</th> 17 <th>Opinion</th> 18 <th colspan="3"></th> 19 </tr> 20 </thead> 21 22 <tbody> 23 <% @books.each do |book| %> 24 <tr> 25 <td> 26 <%= link_to user_path(book.user) do %> 27 <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-mini.jpg") %> 28 <% end %> 29 </td> 30 <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td> 31 <td><%= book.body %></td> 32 </tr> 33 <% end %> 34 </tbody> 35 </table> 36 </div> 37</div> 38 39
↓以下部分テンプレートです
profilehtmlerb
1<div class="row"> 2 <div class="col-xs-3"> 3 <h2>User info</h2> 4 <%= render 'profile', user: @user %> 5 6 <h2>New book</h2> 7 <%= render 'books/form', book: @book %> 8 </div> 9 10 <div class="col-xs-9"> 11 <h2>Books</h2> 12 <!--books一覧 --> 13 <table class="table table-hover table-inverse"> 14 <thead> 15 <tr> 16 <th></th> 17 <th>Title</th> 18 <th>Opinion</th> 19 <th colspan="3"></th> 20 </tr> 21 </thead> 22 23 <tbody> 24 <% @books.each do |book| %> 25 <tr> 26 <td> 27 <%= link_to user_path(book.user) do %> 28 <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-mini.jpg") %> 29 <% end %> 30 </td> 31 <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td> 32 <td><%= book.body %></td> 33 </tr> 34 <% end %> 35 </tbody> 36 </table> 37 <!--books一覧 --> 38 </div> 39</div> 40 41
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/03 12:53