在 Rails 中创建模型的新实例时出现问题,路径丢失



这是我得到的错误:

ActionController::UrlGenerationError in Books#show
Showing /Users/bardiap/saasapp/app/views/chapters/_index.html.erb where line #3 raised:
No route matches {:action=>"show", :controller=>"chapters", :id=>"2"}, missing required keys: [:book_id]

以下是相关文件:

_index.html.erb

<h1>Chapters</h1>
<%= link_to 'New chapter', book_chapter_path %>

  <% @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',book_chapter_path(chapter)%>
        </span>
      </li>
    </ul>
    <%= link_to 'Edit', edit_book_chapter_path(chapter) %>
    <%= link_to 'Destroy', book_chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

相关路线

                   chapters#index     POST   /books/:book_id/chapters(.:format)                                                        chapters#create
                     new_book_chapter GET    /books/:book_id/chapters/new(.:format)                                                    chapters#new
                    edit_book_chapter GET    /books/:book_id/chapters/:id/edit(.:format)                                               chapters#edit
                         book_chapter GET    /books/:book_id/chapters/:id(.:format)                                                    chapters#show
                                      PATCH  /books/:book_id/chapters/:id(.:format)                                                    chapters#update
                                      PUT    /books/:book_id/chapters/:id(.:format)                                                    chapters#update
                                      DELETE /books/:book_id/chapters/:id(.:format)                                                    chapters#destroy
                                books GET    /books(.:format)                                                                          books#index
                                      POST   /books(.:format)                                                                          books#create
                             new_book GET    /books/new(.:format)                                                                      books#new
                            edit_book GET    /books/:id/edit(.:format)                                                                 books#edit
                                 book GET    /books/:id(.:format)                                                                      books#show
                                      PATCH  /books/:id(.:format)                                                                      books#update
                                      PUT    /books/:id(.:format)                                                                      books#update
                                      DELETE /books/:id(.:format)

模式.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

所有这些嵌套路由都需要一个@book对象(检查您的rake routes输出,其中显示:book_id(,而您没有提供该对象。

请参阅指南(在第 2.7.1 节之前(,其中说:

这还将创建路由帮助程序,例如 magazine_ads_url 和 edit_magazine_ad_path。这些帮助程序将 Magazine 的实例作为第一个参数 (magazine_ads_url(@magazine((。

例如,这个:

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

应如下所示:

<%= link_to 'New chapter', new_book_chapter_path(@book) %>

注意:您应该在此处使用new_book_chapter_path,而不是book_chapter_path

这:

<%=link_to '@chapter',book_chapter_path(chapter)%>

应如下所示:

<%=link_to '@chapter', book_chapter_path(@book, chapter)%>

注意:除非您希望链接显示为文字字符串'@chapter',否则该'@chapter'没有任何意义。相反,它可能应该是这样的:

<%=link_to chapter.title, book_chapter_path(@book, chapter)%>
或者,

或者,

<%=link_to chapter.title, [@book, chapter] %>

您应该在控制器中实例化 @book 变量(如上一个问题所示(。

最新更新