エラー内容
NoMethodError in Books#index
Showing /home/ec2-user/environment/Bookers/app/views/books/index.html.erb where line #14 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #14):
12
13
14
15
16
17
### views
<tbody> <% @list.each do |list| %> <div class="book-item"> <div class="book-image"><%= image_tag('img/forest.jpg', class: 'book-image') %></div> <section class="book-text"> <h3 class="book-text-heading"><%= link_to book.title, book_path(book) %></h3> <p class="book-date"><%= book.created_at %></p> <span class="category"><%= book.category %></span> </section> </div> <% end %> </tbody>コントローラー
class BooksController < ApplicationController
def top
end
def edit
end
def new
@list = List.new
end
def create
list = List.new(list_params)
list.save
redirect_to books_path(list.id)
end
def index
@lists = List.all
end
def show
@list = List.find(params[:id])
end
private
def list_params
params.require(:list).permit(:title, :body)
end
end
ルート
Rails.application.routes.draw do
get 'todolists/new'
get 'top' => 'homes#top'
post 'todolists' => 'todolists#create'
get 'todolists' => 'todolists#index'
get 'todolists/:id' => 'todolists#show', as: 'todolist'
get 'books/:id' => 'books#show', as: 'book'
get 'homes/index'
get 'homes/show'
get 'homes/new'
get 'homes/edit'
get 'top' => 'books#top'
get 'books/index'
get 'books/show'
get 'books/new'
get 'books/edit'
get 'books' => 'books#index'
get 'books' => 'homes#top'
For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
エラーの原因また解決方法を教えてください。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー