Search code examples
ruby-on-railsroutesactiveresource

Rails nested resources within a namespace


I have this in my view:

form_for [:support, @thread, @reply], url: support_thread_replies_path do |f|

And this in my routes.rb:

namespace :support do
  resources :threads do
    resources :replies
  end
end

That doesn't work:

Routing Error

No route matches {:action=>"new", :controller=>"support/replies"}

If I remove the url: key from my form_for, I just get a NoMethodError when the form helper tries to call an undefined path helper method:

I get the same Routing Error even when I remove the :support symbol from the beginning of the array in my sample view code (using :support was suggested by an answer to another similar question here)


Solution

  • Pass an instance of Thread as the first parameter in the path helper:

    support_thread_replies_path(@thread)
    

    That way Rails knows what thread you're creating a new reply for.

    I believe you should be able to do this without the :url key or the path helper at all, though.