前提・実現したいこと
railsで現在投稿機能を作成しています
発生している問題・エラーメッセージ
本を投稿後一覧画面に表示したいのですが
undefined method `each' for nil:NilClass
といったエラーが出てしまい解決ができません
該当のソースコード
Show.html
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <link rel="stylesheet" href="bookers2/app/assets/stylesheets/users.scss"> 6 <script src="https://kit.fontawesome.com/34c9407723.js" crossorigin="anonymous"></script> 7</head> 8<main class="user"> 9 <p> 10 <% if flash[:notice] %> 11 <p id="notice"> 12 <%= flash[:notice] %> 13 </p> 14 <% end %> 15 <div class="container"> 16 <div class="row"> 17 <div class="col-lg-4"> 18 <h2> 19 User info 20 </h2> 21 <div class="userimg" src="assets/images/bou.jpg"></div> 22 <table class="table"> 23 <tbody> 24 <tr></tr> 25 <th> 26 name 27 </th> 28 <th> 29 </th> 30 <tr></tr> 31 <th> 32 introduction 33 </th> 34 <th> 35 36 </th> 37 </tbody> 38 </table> 39 <div class="edit"> 40 <button type="button" class="btn btn-outline-secondary btn-block"> 41 <i class="fas fa-user-cog"></i> 42 </button> 43 <h2> 44 New book 45 </h2> 46 <td class="form"> 47 <%= form_with model:@user, local:true do |f| %> 48 <p4>Title</p4><br> 49 <%= f.text_field :Title %><br> 50 <p4>Opinion</p4><br> 51 <%= f.text_field :Opinion %><br> 52 <button type="submit" class="btn btn-lg btn-success"> 53 Create book 54 </button> 55 <% end %> 56 </td> 57 </div> 58 </div> 59 <div class="col-lg-8"> 60 <h2>Book detail</h2> 61 <table class="table"> 62 <% @book.each do |book| %> 63 <thead> 64 <tr> 65 66 </tr> 67 </thead> 68 <tbody> 69 </tbody> 70 <% end %> 71 </div> 72 </div> 73 </div> 74 </html>
books/index_html
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <link rel="stylesheet" href="bookers2/app/assets/stylesheets/users.scss"> 6 <script src="https://kit.fontawesome.com/34c9407723.js" crossorigin="anonymous"></script> 7</head> 8<main class="user"> 9 <p> 10 <% if flash[:notice] %> 11 <p id="notice"> 12 <%= flash[:notice] %> 13 </p> 14 <% end %> 15 <div class="container"> 16 <div class="row"> 17 <div class="col-lg-8"> 18 <h2> 19 User info 20 </h2> 21 <div class="userimg" src="assets/images/bou.jpg"></div> 22 <table class="table"> 23 <tbody> 24 <tr></tr> 25 <th> 26 name 27 <%= current_user.name %> 28 </th> 29 <th> 30 </th> 31 <tr></tr> 32 <th> 33 introduction 34 <%= current_user.introduction %> 35 </th> 36 <th> 37 38 </th> 39 </tbody> 40 </table> 41 <div class="edit"> 42 <button type="button" class="btn btn-outline-secondary btn-block"> 43 <i class="fas fa-user-cog"></i> 44 </button> 45 <h2> 46 New book 47 </h2> 48 <td class="form"> 49 <%= form_with model:@book, local:true do |f| %> 50 <p4>Title</p4><br> 51 <%= f.text_field :title %><br> 52 <p4>Opinion</p4><br> 53 <%= f.text_area :body %> 54 <%= f.submit 'Create book' %> 55 <% end %> 56 </td> 57 </div> 58 </div> 59 <div class="col-lg-4"> 60 <h2>Book detail</h2> 61 <table class="table"> 62 <thead> 63 <th> 64 Title 65 </th> 66 <th> 67 Opinion 68 </th> 69 </thead> 70 <tbody> 71 <% @books.each do |book| %> 72 <tr> 73 <td> 74 </td> 75 <td> 76 <%= book.title %> 77 </td> 78 <td> 79 <%= book.body %> 80 </td> 81 </tr> 82 <% end %> 83 </tbody> 84 </table> 85 </div> 86 </div> 87 </div> 88 </html>
books_controller
1class BooksController < ApplicationController 2 def new 3 @book = Book.new 4 end 5 6 def create 7 @book = Book.new(book_params) 8 @book.user_id = current_user.id 9 @book.save 10 redirect_to book_path(id: current_user) 11 end 12 13 def index 14 @books = Book.all 15 @book = Book.all 16 @book = Book.new 17 end 18 19 def show 20 @books = Book.find_by(id:params[:id]) 21 @users_id = params[:id] 22 end 23 24 def destroy 25 end 26 27 private 28 29 def book_params 30 params.require(:book).permit(:title, :body) 31 end 32end 33
rails 5
よろしくお願いいたします
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。