Let's say I have a model class like this
class Parent < ActiveRecord::Base has_many :things has_one :special_thing, :classname => 'Thing', ... end
and I want the associations to be handled by UserThingsController and UserSpecialThingsController respectively. Theny, apparently, I have to set up routes with explicit :controllers
map.resources :parents do parent| parent.resources :things, :controller => :user_things parent.resource :special_thing, :controller => :user_special_things end
Or is there a way to get this automatically?
As I want to edit the nested resources in-place on the same page as their parent object, I have a view, that contains a forms like these
<% form_for(@special_thing, :url => image_path) do |f| %> <%= f.text_field :name %>
<% form_for(@things[0], :url => image_path) do |f| %> <%= f.text_field :name %>
The first thing to notice is that I have to give the :url argument, for otherwise both forms would go to ThingsController instead of the "nested" controllers.
Second, the element ids and names are unusable. What I get is
<form action="/parent/1/special_thing" id="new_thing" method="post"> <input id="thing_name" name="thing[name]" type="text" />
Whereas what I need is, for the has_one case,
<form action="/parent/1/special_thing" id="new_thing" method="post"> <input id="parent_special_thing_name" name="parent_special_thing[name]" type="text" />
and for the has_many case with the appropriate object ids used in names/ids.
I just can't get this stuff to do (easily) what I think it ought to do. That may well be an indication that I'm misunderstanding something considerably. What's the right way?
Michael