如何访问相关方法

时间:2018-02-01 20:13:34

标签: ruby-on-rails ruby

所以我有一本课本,我想在其下面渲染所有相关的章节,我该怎么做?这是我到目前为止的代码以及我收到的错误消息。

错误:Books#show中的NoMethodError 显示/Users/bardiap/saasapp/app/views/chapters/_index.html.erb第6行引出:

未定义的方法`each'代表nil:NilClass

章节/ _index.html.erb

<h1>Chapters</h1>

<%= link_to 'New chapter', new_chapter_path %>


  <% @chapters.each do |chapter| %>
    <%=link_to 'Show',chapter_path(chapter)%>
    <%= link_to 'Edit', edit_chapter_path(chapter) %>
    <%= link_to 'Destroy', chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

书籍/ _show.html.erb

<h1>
  <%= @book.title %>
</h1>

<%= render 'chapters/index' %>


<p>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
</p>

书籍控制器

class BooksController < ApplicationController
  def show
    @book =Book.find(params[:id])
  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 ChaptersController < ApplicationController
  def show
    @chapter =Chapter.find(params[:id])
  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
      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

schema.rb

create_table "books", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "bookcover_file_name"
    t.string "bookcover_content_type"
    t.integer "bookcover_file_size"
    t.datetime "bookcover_updated_at"
    t.string "authorpic_file_name"
    t.string "authorpic_content_type"
    t.integer "authorpic_file_size"
    t.datetime "authorpic_updated_at"
    t.string "author"
    t.string "month"
  end

  create_table "chapters", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "book_id"
    t.index ["book_id"], name: "index_chapters_on_book_id"
  end

create_table "books", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "bookcover_file_name"
    t.string "bookcover_content_type"
    t.integer "bookcover_file_size"
    t.datetime "bookcover_updated_at"
    t.string "authorpic_file_name"
    t.string "authorpic_content_type"
    t.integer "authorpic_file_size"
    t.datetime "authorpic_updated_at"
    t.string "author"
    t.string "month"
  end

  create_table "chapters", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "book_id"
    t.index ["book_id"], name: "index_chapters_on_book_id"
  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

章节模型

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

2 个答案:

答案 0 :(得分:2)

您收到此错误是因为@chapters控制器books操作中未定义show。由于您需要与该书相关联的所有章节,因此请更改

<%= render 'chapters/index' %>

<%= render 'chapters/index', chapters: @book.chapters %>

并在chapters/_index中,更改

<% @chapters.each do |chapter| %>
    <%=link_to 'Show',chapter_path(chapter)%>
    <%= link_to 'Edit', edit_chapter_path(chapter) %>
    <%= link_to 'Destroy', chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

<% chapters.each do |chapter| %>
    <%=link_to 'Show',chapter_path(chapter)%>
    <%= link_to 'Edit', edit_chapter_path(chapter) %>
    <%= link_to 'Destroy', chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

希望这会有所帮助。

答案 1 :(得分:0)

您需要将变量传递给部分

 <%= render 'chapters/index', chapters: @chapters %>

然后在部分中使用变量chapters