ruby-on-railsnamespacesruby-on-rails-3form-for

Nested resources in namespace form_for


Problem

The form_for helper incorrectly determines the path to my nested resource inside of a namespace. The models in question are: Forum::Thread and Forum::Reply respectively, located in a subfolder called "forum" under my models directory. This is in Rails 3 BETA 3.

routes.rb

  namespace :forum do
    root :to => 'threads#index'
    resources :threads do
      resources :replies
    end
  end

app/views/forum/replies/_form.html.haml

...
  - form_for [@thread, @reply] do |f|
...

app/controllers/forum/replies_controller.rb

...
  def new
    @reply = Forum::Reply.new
  end
...

Error

undefined method `forum_thread_forum_replies_path'

In reference to the line outlined above in _form.html.haml


Solution

  • Editted solution in case people don't read the reactions:

    <%= form_for [:admin, @person, @image] do |f| %>
    

    Old response:

    I have a project with an admin namespace and People and Images resources, this is the way I build my form_for in rails3, I haven't found a way just yet to do it cleaner...

    <%= form_for [@person, @image], :url => admin_person_images_path do |f| %>