继续获取嵌套资源视图的ActionController :: UnknownFormat

时间:2018-02-02 00:01:30

标签: ruby-on-rails

每次我去这个链接/ books / 2 /章我都会收到这个错误:

ChaptersController #index缺少此请求格式和变体的模板。 request.formats:[" text / html"] request.variant:[]

请告诉我出错的地方以及我可以对我的代码进行的任何其他改进。

这些是我的控制器

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

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

这些是我的模特

class Chapter < ApplicationRecord
  has_many :sections, dependent: :destroy
  belongs_to :book
  validates :title, presence: true,
                    length:{minimum: 5}
end

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

这些是我的路线

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

这是我的章节/ _index.html.erb文件

<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%>

这是我的books / show.html.erb文件

<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>

1 个答案:

答案 0 :(得分:1)

为什么章节/ _index而不是章节/索引?这一定是错误。

我认为您的控制器中存在一些错误。

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

修改

我看到你正在使用chapter / _index作为book / show的部分内容。但是你也在ChaptersController #index中使用相同的东西。虽然不是很好,但你可以这样做:

章节/ index.html.erb

<%= render partial: 'chapters/_index', locals: { book: @book, chapters: @chapters } %>

在章节/ _index中,用章节替换@chapters(不带@)和@book by book(不带@)