Failure/Error: @user = User.find(current_user.id)
NoMethodError: undefined method `id' for nil:NilClass
のエラーが解決できず詰まってしまいました。
解決法を教えてください
bocontroller
1class BooksController < ApplicationController 2 3 def show 4 @book = Book.find(params[:id]) 5 @user = @book.user 6 @book_new =Book.new 7 end 8 9 def index 10 @user = User.find(current_user.id) 11 @books = Book.all 12 @book = Book.new 13 end 14 15 def create 16 @book = Book.new(book_params) 17 @book.user_id = current_user.id 18 if @book.save 19 redirect_to book_path(@book.id), notice: "You have created book successfully." 20 else 21 @user = User.find(current_user.id) 22 @books = Book.where(user_id: @user.id) 23 render 'users/show' 24 end 25 end 26 27 def edit 28 @book = Book.find(params[:id]) 29 if @book.user_id != current_user.id 30 redirect_to books_path 31 end 32 end 33 34 35 36 def update 37 @book = Book.find(params[:id]) 38 if @book.update(book_params) 39 redirect_to book_path(@book), notice: "You have updated book successfully." 40 else 41 render "books/edit" 42 end 43 end 44 45 def destroy 46 @book = Book.find(params[:id]) 47 @book.destoy 48 redirect_to books_path 49 end 50 51 private 52 53 def book_params 54 params.require(:book).permit(:title, :body, :user_id) 55 end 56end
userscontroller
1class UsersController < ApplicationController 2 before_action :ensure_correct_user, only: [:update] 3 4 def show 5 @user = User.find(params[:id]) 6 @books = Book.where(user_id: @user.id) 7 @book = Book.new 8 end 9 10 def index 11 @user = User.find(current_user.id) 12 @book = Book.new 13 @users = User.all 14 end 15 16 def edit 17 @user = User.find(params[:id]) 18 if params[:id].to_i != current_user.id 19 redirect_to user_path(current_user.id) 20 else 21 @user = User.find(current_user.id) 22 end 23 end 24 25 def update 26 @user = User.find(params[:id]) 27 if @user.update(user_params) 28 redirect_to users_path(@user), notice: "You have updated user successfully." 29 else 30 render "show" 31 end 32 end 33 34 private 35 def user_params 36 params.require(:user).permit(:name, :introduction, :profile_image) 37 end 38 39 def ensure_correct_user 40 @user = User.find(params[:id]) 41 unless @user == current_user 42 redirect_to user_path(current_user) 43 end 44 end 45end 46
booksshow
1div class="row"> 2 <div class="col-xs-3"> 3 4 <h2>User info</h2> 5 <%= render 'users/profile', user: @user %> 6 7 <h2>New book</h2> 8 <%= render 'books/newform', book: @book_new %> 9 10 </div> 11 12 <div class="col-xs-9"> 13 <h2>Book detail</h2> 14 15 <table class="table"> 16 <% if @user.id == current_user.id %> 17 <td> 18 <%= link_to user_path(@user.id) do %> 19 <%= attachment_image_tag @user, :profile_image, size:"40x40", class: "attachment user profile_image fallback", fallback: "no_image.jpg" %> 20 <br> 21 <%= @user.name %> 22 <% end %> 23 </td> 24 <td><%= link_to @book.title, book_path %></td> 25 <td><%= @book.body %></td> 26 <td><%= link_to "Edit", edit_book_path(@book.id), class: "btn-sm btn-success edit_book_30617" %></td> 27 <td><%= link_to "Destroy", book_path(@book.id), class: "btn-sm btn-danger destroy_book_30617", method: :delete, "data-confirm" => "本当に消しますか?" %></td> 28 <% else %> 29 <td> 30 <%= link_to user_path(@user.id) do %> 31 <%= attachment_image_tag @user, :profile_image, size:"40x40", class: "attachment user profile_image fallback", fallback: "no_image.jpg" %> 32 <br> 33 <%= @user.name %> 34 <% end %> 35 </td> 36 <td><%= link_to @book.title, book_path %></td> 37 <td><%= @book.body %></td> 38 <% end %> 39 </table> 40 </div>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/01 08:05
2020/08/01 08:09