# Nested resources: controllers and form input names

**URL:** <https://www.ruby-forum.com/t/nested-resources-controllers-and-form-input-names/120480>\
**Category:** Rails\
**Created:** [November 17, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/nested-resources-controllers-and-form-input-names/120480 "2007-11-17T12:14:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [November 17, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/nested-resources-controllers-and-form-input-names/120480/1 "2007-11-17T12:14:09Z")

</div>

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

Whereas what I need is, for the has\_one case,

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

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
