在 table 之后尝试使用 simple_form 时出错

Error while trying to use simple_form following a table

Rails 6
simple_form
bootstrap 4

我有一个 table,在我的 views/books/index.htmlslim 中,我想添加一个 simple_form,如下所示:

table.table.table-striped
  thead
    tr
      th Name
  tbody
    - @books.each do |book|
      tr
        td = book.name

= simple_form_for(@book) do |f|
  .form-inputs
    = f.input :book_name

  .form-actions
    = f.button :submit

但是,我收到一条错误消息,告诉我:

ActionView::Template::Error (undefined method `model_name' for nil:NilClass):

假设是因为这是索引视图。我该如何解决这个问题?

编辑:

澄清一下,当我提交表单时,我希望它转到书本新控制器操作,而不是索引操作,所以我正在寻找等效于:

= form_with url: new_book_url do |f|

您需要在 index 操作中设置 @book 实例变量:

def index
  @book = Book.new
  # rest of the code, setting @books etc.
end