簡體   English   中英

無法在Rails中創建模型的新實例,路徑丟失了嗎?

[英]Trouble create new instance of model in Rails, path missing?

這是我得到的錯誤:

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)

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

所有這些嵌套路由都需要一個@book對象(檢查您的rake routes輸出中是否顯示:book_id ),而您沒有提供該對象。

請參閱指南 (位於第2.7.1節之前),其中指出:

這還將創建路由助手,例如magazine_ads_url和edit_magazine_ad_path。 這些助手將Magazines實例作為第一個參數(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變量(如上一個問題中所示)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM