Reputation: 71
Every time I go to this link /books/2/chapter I get this error:
ChaptersController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
Please show me where I am going wrong and any other improvements I can make to my code.
These are my controllers
class ChaptersController < ApplicationController
def show
@chapter =Chapter.find(params[:id])
@sections = Section.all
end
def index
@chapters = Chapter.all
@book = Book.find(params[:book_id])
end
def new
@chapter = Chapter.new
@book = Book.find(params[:book_id])
end
def edit
@chapter = Chapter.find(params[:id])
end
def create
@chapter = Chapter.new(chapter_params)
@book = Book.find(params[:book_id])
if @chapter.save
redirect_to @chapter
else
render 'new'
end
end
def update
@chapter = Chapter.find(params[:id])
if @chapter.update(chapter_params)
redirect_to @chapter
else
render 'edit'
end
end
def destroy
@chapter = Chapter.find(params[:id])
@chapter.destroy
redirect_to chapters_path
end
private
def chapter_params
params.require(:chapter).permit(:title,:text)
end
end
and
class BooksController < ApplicationController
def show
@book = Book.find(params[:id])
@chapters = Chapter.all
end
def index
@books = Book.all
end
def new
@book = Book.new
end
def edit
@book = Book.find(params[:id])
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to @book
else
render 'new'
end
end
def update
@book = Book.find(params[:id])
if @book.update(book_params)
redirect_to @book
else
render 'edit'
end
end
def destroy
@book = Book.find(params[:id])
@book.destroy
redirect_to books_path
end
private
def book_params
params.require(:book).permit(:title,:text,:bookcover,:authorpic,:author)
end
end
These are my models
class Chapter < ApplicationRecord
has_many :sections, dependent: :destroy
belongs_to :book
validates :title, presence: true,
length:{minimum: 5}
end
and
class Book < ApplicationRecord
has_many :chapters, dependent: :destroy
has_attached_file :bookcover, styles: { medium: "300x300>", thumb: "100x100>" }
has_attached_file :authorpic, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :bookcover, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment_content_type :authorpic, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates :title, presence: true,
length:{minimum: 5}
end
These are my routes
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts, only: :create
get 'contact-us', to: 'contacts#new', as: 'new_contact'
get 'bookclub', to: 'pages#bookclub'
get 'podcast', to: 'pages#podcast'
resources :essays do
resources :comments
end
resources :podcasts do
resources :podcomments
end
resources :books do
resources :chapters do
resources :sections do
resources :bookcomments
end
end
end
end
This is my chapters/_index.html.erb file
<div class="mdl-grid">
<div class="mdl-cell mdl-cell--4-col"><h3>Chapters</h3>
</div>
</div>
<%= link_to 'New chapter', new_book_chapter_path(@book) %>
<% @chapters.each do |chapter| %>
<ul class="demo-list-item mdl-list">
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<%=link_to chapter.title, book_chapter_path(@book, chapter)%>
</span>
</li>
</ul>
<%= link_to 'Edit', edit_book_chapter_path(@book, chapter) %>
<%= link_to 'Destroy', book_chapter_path(@book, chapter),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<%end%>
This is my books/show.html.erb file
<div class="mdl-grid">
<div class="mdl-cell mdl-cell--4-col"><h3><%= @book.title %></h3>
</div>
<%= render 'chapters/index', chapters: @chapters %>
<p>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
</p>
Upvotes: 0
Views: 255
Reputation: 3005
Why chapters/_index and not chapters/index? This must be the error.
I think you have some errors in your controllers.
def index
# Do you really want all chapters? (from all books)
# @chapters = Chapter.all
@book = Book.find(params[:book_id])
# I think you want only this book's chapters
@chapters = @book.chapters
end
def show
@chapter =Chapter.find(params[:id])
# The same thing. You want only sections from this chapter
# @sections = Section.all
@sections = @chapter.sections
end
EDIT
I see you are using chapters/_index as a partial from book/show. But you are also using the same in ChaptersController#index. Although not very nice you could do this:
chapters/index.html.erb
<%= render partial: 'chapters/_index', locals: { book: @book, chapters: @chapters } %>
in chapters/_index, replace @chapters by chapters (without @) and @book by book (without @)
Upvotes: 1