我有书籍,章节嵌套在下面,章节中嵌套的每一章都有问题,而问题中嵌套了评论。
当我点击书籍页面上的新章节时,如何将其保存到与其相关联的书籍中,以及如何获取书籍#show page以遍历所有章节标题(包含每章的链接)显示页面。)
这是错误:未定义的方法`title'为零:NilClass。参考此文件:Users / bardiap / saasapp / app / views / books / show.html.erb
<div class="homepage">
<h1><%= @book.title %></h1>
<%= link_to 'New chapter', new_book_chapter_path(@book, @chapter) %>
<% @chapters.each do |chapter| %>
<h3><%= link_to chapter.title, chapter %></h3>
<% end %>
<p>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Destroy', book_path(@book),
method: :delete,
data: { confirm: 'Are you sure?' } %> |
<%= link_to 'Back', books_path %> |
</p>
</div>
这是我的书籍和章节控制器:
class BooksController < ApplicationController
def show
@book = Book.find(params[:id])
@chapters = Chapter.where(book_id: @book.id).order("created_at ASC")
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,:author)
end
end
章
def show
@chapter = Chapter.find(params[:id])
@questions = question.where(chapter_id: @chapter.id).order("created_at ASC")
end
def index
@chapters = Chapter.all
end
def new
@chapter = Chapter.new
end
def edit
@chapter = Chapter.find(params[:id])
end
def create
@chapter = Chapter.new(chapter_params)
if @chapter.save
render 'books/show'
else
render 'books/show'
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
以下是模型和路线
class Book < ApplicationRecord
has_many :chapters
validates :title, presence: true,
length:{minimum: 5}
end
class Chapter < ApplicationRecord
has_many :questions
belongs_to :book
validates :title, presence: true,
length:{minimum: 5}
end
路线
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
get 'about', to: 'pages#about'
get 'terms', to: 'pages#terms'
get 'privacy', to: 'pages#privacy'
resources :contacts, only: :create
resources :podcasts
resources :books do
resources :chapters, shallow: true
end
resources :chapters do
resources :questions, shallow: true
end
resources :questions do
resources :comments, module: :questions, shallow: true
end
resources :users do
resource :profile
resources :comments
end
get 'contact-us', to: 'contacts#new', as: 'new_contact'
get 'podcast', to: 'pages#podcast'
resources :essays do
resources :comments, module: :essays, shallow: true
end
end
观点:
章节/ _form.html.erb
<%= form_for [@book, @chapter] do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit "Submit" %>
</p>
<% end %>
章节/ index.html.erb
<div class="homepage">
<button class="mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab">
<i class="material-icons"><%= link_to "add", new_chapter_path%></i>
</button>
<div class="mdl-grid">
<% @chapters.each do |chapter| %>
<div class="mdl-cell mdl-cell--4-col">
<div class="demo-card-event mdl-card mdl-shadow--2dp">
<div class="mdl-card__title mdl-card--expand">
<h4>
<%=chapter.title%>
</h4>
</div>
<div class="mdl-card__actions mdl-card--border">
<%=link_to 'VIEW BOOK',chapter_path(chapter), class: "mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect"%>
<div class="mdl-layout-spacer"></div>
<i class="material-icons">chapter</i>
</div>
</div>
</div>
<% end %>
</div>
<% @chapters.each do |chapter| %>
<%= link_to 'Edit', edit_chapter_path(chapter) %> |
<%= link_to 'Destroy', chapter_path(chapter),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<% end %>
<br><br><br><br><br>
</div>
章节/ show.html.erb
<div class="homepage">
<h3><%= @chapter.title %></h3>
<h5><%= @chapter.text %></h5>
<p>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Destroy', book_path(@book),
method: :delete,
data: { confirm: 'Are you sure?' } %> |
<%= link_to 'Back', books_path %> |
</p>
</div>
我哪里出错了,还有其他方法可以让我的代码更好用。
答案 0 :(得分:1)
在图书/ show:
中创建图书中的新章节<%= link_to 'New chapter', new_book_chapter_path(@book) %>
(无需传递章节,因为它将被创建)。然后,在ChaptersController中#new:
def new
@book = Book.find_by(id: params[:book_id]
if @book
@chapter = @book.chapters.build
end
end
您还可以更改BooksController#show
def show
@book = Book.find_by(id: params[:id])
if @book
@chapters = @book.chapters.order("created_at ASC")
end
end