Chris Nicola
Chris Nicola

Reputation: 14574

form_for routing problem with nested resource

Seeing a problem with using form_for with a nested resource where I have remapped the route to be more sensible.

My routes.rb:

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

And my _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

This is resulting in this error:

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

What I'm expecting is book_sections_path but it is failing to take into account the setting in routes.rb.

Upvotes: 1

Views: 963

Answers (1)

Peter Brown
Peter Brown

Reputation: 51697

Since there is no real relationship between models and controllers, then you will need to specify the URL when not using standard conventions:

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

Upvotes: 3

Related Questions