2011-09-11 160 views
1

看到一個使用form_for與嵌套資源的問題,我已經重新映射路由更明智。form_for嵌套資源路由問題

我的routes.rb:

resources :books do 
    resources :sections, :controller => 'content_sections' 
    member do 
    post 'publish' 
    end 
end 

而且我_form.html.haml

= form_for [@book, @content_section] do |f| 
    -if @content_section.errors.any? 
    #error_explanation 
     %h2= "#{pluralize(@content_section.errors.count, "error")} prohibited this section from being saved:" 
     %ul 
     - @content_section.errors.full_messages.each do |msg| 
      %li= msg 

    .field 
    = f.label :name 
    = f.text_field :name 

這是造成此錯誤:

undefined method `book_content_sections_path' for #<#<Class:0x00000103a58238>:0x00000103a4a0e8> 

什麼我期待的book_sections_path但它沒有考慮到routes.rb中的設置。

+0

請加耙路輸出 –

回答

3

既然有模型和控制器之間沒有真正的關係,那麼你將需要不使用標準約定時指定的網址:

form_for [@book, @content_section], :url => book_sections_path(@book, @content_section) 
+1

謝謝,對不起,我有點新我發現文檔過於集中於這些約定,這些約定在你進入真實世界的5分鐘內是很棒的。奇怪的是我發誓我有這個工作,然後當我將BookSection重命名爲ContentSection(模型,控制器相關的視圖被重命名)時,它停止了。你會認爲它可以適應路線設置。 –

+2

實際上,與此有關的許多問題是有兩個路徑,具體取決於您是否正在創建或編輯: –