railsで作成中の画像にリンクを貼り付けたい
現在、railsで画像にリンクを張り付けshow(一覧ページ)に飛ばすリンクを作成中なのですが、以下のエラーが出ています。
発生している問題・エラーメッセージ
NoMethodError in Books#show
Showing /vagrant/bookers-lv2/app/views/books/show.html.erb where line #44 raised:
undefined method `user_id' for nil:NilClass
該当のソースコード
<table class="table table-book" style="width: 70%;"> <thead> <h2>Book detail</h2> </thead> <tbody><tr> <% @books.each do |book| %> <td><%= link_to user_path(book.user.user_id) do %> <%= attachment_image_tag @user, :profile_image %> <%= book.user.name %> <% end %></td> <td><%= book.title %></td> <td><%= book.body %></td> <% end %> </tr> </tbody> </table>
以下はbookのコントローラーです。
class BooksController < ApplicationController
def index
@user = User.find(current_user.id)
@users = User.all
@books = Book.all
@book = Book.new
end
def show
@user = User.find(current_user.id)
@users = User.all
@books = Book.all
@newbook = Book.new
end
def create
book = Book.new(book_params)
book.save
flash[:success]='You have created book successfully.'
redirect_to users_detail_path(book.id)
end
def edit
@book = Book.find(params[:id])
end
def update
book = Book.new(book_params)
book.save
flash[:success]='You have updated book successfully.'
redirect_to books_path(book.id)
end
def destroy
book= Book.find(params[:id])
book.destroy
redirect_to books_path
end
private
def book_params
params.require(:book).permit(:title, :body)
end
end
試したこと
このファイルはuserとbookの2つのモデルがあり、1対多の関係です。link_toの指定先はshowページでbookのモデルに結びついたuserのidを指定したのですが、エラーが発生しました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。