発生している問題・エラーメッセージ
現在本の投稿アプリを作成中なのですが以下のようなエラーが発生し、解決することができません。
どなたかご教授いただけませんでしょうか。
NoMethodError in Books#show Showing /home/vagrant/work/bookers_new/app/views/books/show.html.erb where line #24 raised: undefined method `name' for nil:NilClass <tr> <td>name</td> <td><%= @book.user.name %></td>/この部分が赤くなっています </tr>
該当のソースコード
ruby
1app/views/books/show.html.erb 2 3<div class="flash"> 4<% flash.each do |key, value| %> 5 <%= content_tag(:div, value, class: "#{key}") %> 6<% end %> 7</div> 8 9 10<div class="container"> 11 <div class="row"> 12 <div class="col-lg-3" style="background-color: white"> 13 14 <h2>User info</h2> 15 16 <%= attachment_image_tag @user, :profile_image, :fill, 100, 100, fallback: "/assets/sample_image.gif" ,format: '/assets/sample_image.gif' %> 17 18 <table class="table table-hover"> 19 20 <tbody> 21 22 <tr> 23 <td>name</td> 24 <td><%= @book.user.name %></td> 25 </tr> 26 27 <tr> 28 <td>Introduction</td> 29 <td><%= @book.user.introduction %></td> 30 </tr> 31 32 </tbody> 33 34 </table> 35 36<%= link_to(edit_user_path(@book.user.id), class: "btn btn-default") do %> 37 <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> 38<% end -%> 39 40<h1>New book</h1> 41 42<%= form_for(@book) do |f| %> 43 44 <p>Title</p> 45 <%= f.text_field :title , class: "edit-title"%> 46 47 <p>Body</p> 48 <%= f.text_area :body, class: "edit-body"%> 49 <br> 50 <%= f.submit 'Create Book', class: "btn btn-primary" %> 51 52<% end %> 53 54 </div> 55 56<div class="col-lg-9"> 57 <h1>Book detail</h1> 58 59 <table class="table table-striped"> 60 <thead> 61 </thead> 62 <tbody> 63 <div class="flash"> 64 <% flash.each do |key, value| %> 65 <%= content_tag(:div, value, class: "#{key}") %> 66 <% end %> 67 </div> 68 69 70 <table class="table table-hover"> 71 <thead> 72 </thead> 73 <tbody> 74 <tr> 75 <td><span><%= attachment_image_tag @book.user, :profile_image, :fill, 50, 50, format: 'jpeg' %></span></td> 76 <td><%= @book.title %></td> 77 <td><%= @book.body %></td> 78 79 <!-- もしも、ログインしているユーザーと、投稿したユーザーが合致したら編集と削除をするボタンをだします --> 80 <% if current_user.id == @book.user_id %> 81 <td><h4><span class="label label-success"><%= link_to "edit", edit_book_path(@book.id) %></span></h4></td> 82 <td><h4><span class="label label-danger"><%= link_to "destroy", book_path(@book), method: :delete, "data-confirm" =>"Are you sure?" %></span></h4></td> 83 <% else %> 84 <td></td> 85 <td></td> 86 87 <% end %> 88 </tr> 89 </tbody> 90</table> 91</div>
ruby
1class BooksController < ApplicationController 2 before_action :authenticate_user! 3 4 5 6 def create 7 @book = Book.new(book_params) 8 @book.user_id = current_user.id 9 if@book.save 10 redirect_to book_path(@book.id) 11 else 12 @books = Book.all 13 render :show 14 end 15 end 16 17 def index 18 @books = Book.all 19 @books = Book.page(params[:page]).reverse_order 20 @book = Book.new 21 22 end 23 24 def show 25 @book = Book.find(params[:id]) 26 @books = Book.all 27 @book = Book.new 28 29 end 30 31 def usershow 32 @book = Book.find(params[:id]) 33 @books = Book.all 34 @book = Book.new 35 end 36 37 def edit 38 @book = Book.find(params[:id]) 39 end 40 41 def update 42 book = Book.find(params[:id]) 43 book.update(book_params) 44 redirect_to book_path(book.id) 45 flash[:notice] = "Book was successfully update" 46 end 47 48 def destroy 49 book = Book.find(params[:id]) 50 book.destroy 51 redirect_to new_book_path 52 flash[:notice] = "Book was successfully destroyed" 53 end 54 55 def userinfo 56 @book = Book.new 57 end 58 59 private 60 def book_params 61 params.require(:book).permit(:title, :body, :image) 62 end 63end 64
book.rb class Book < ApplicationRecord attachment :profile_image belongs_to :user validates :title, presence: true validates :body , presence: true validates :body , length: { maximum: 200 } end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。