form_for嵌套资源的路由问题

时间:2011-09-11 08:15:11

标签: ruby-on-rails ruby-on-rails-3.1

看到使用带有嵌套资源的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中的设置。

1 个答案:

答案 0 :(得分:3)

由于模型和控制器之间没有真正的关系,因此在不使用标准约定时需要指定URL:

form_for [@book, @content_section], :url => book_sections_path(@book, @content_section)
相关问题