# Need help with nested forms and habtm

**URL:** <https://www.ruby-forum.com/t/need-help-with-nested-forms-and-habtm/181921>\
**Category:** Rails\
**Created:** [January 20, 2010, 9:15am UTC](https://www.ruby-forum.com/t/need-help-with-nested-forms-and-habtm/181921 "2010-01-20T09:15:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilskassube](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/db5fbb/32.png) [@nilskassube](https://www.ruby-forum.com/u/nilskassube)\
**Post date:** [January 20, 2010, 9:15am UTC](https://www.ruby-forum.com/t/need-help-with-nested-forms-and-habtm/181921/1 "2010-01-20T09:15:49Z")

</div>

Hi,

I’m currently stuck with a problem I just can’t find a clever solution  
for. I checked every tutorial, issuetracker, API reference I could  
find but found nothing to “enlighten” me --\> I desperately need your  
help :-).

My Question is: is it possible to use nested forms for habtm  
associations?

–\> Here some more info about what I wanted to do and what failed

### Basic info

I’ve got following model associations:  
Customer  
has\_many :inquiries  
attr\_accessible :inquiry\_ids  
accepts\_nested\_attributes\_for :inquiries

Inquiry  
belongs\_to :customer  
has\_and\_belongs\_to\_many :machines  
accepts\_nested\_attributes\_for :machines  
attr\_accessible :machine\_ids

Machine  
has\_and\_belongs\_to\_many :inquiries

I’m on Rails 2.3.5 and formtastic 0.9.7

### What I want

I wanted to build a nested form (using formtastic + haml), so a User  
could enter his data and create an inquiry regarding several machines  
(he put into a cart in advance)

so I set up this:

- semantic\_form\_for @customer do |form|
  - form.inputs do  
= form.input :companyname  
= form.input :salutation, :as =\> :radio, :label =\> “Gender”,  
:collection =\> [[“Mister”, “m”], [“Mrs.”, “f”]]  
= form.input :first\_name  
= form.input :last\_name  
= form.input :street  
= form.input :zip  
= form.input :city  
= form.input :phone  
= form.input :mobile  
= form.input :fax  
= form.input :email

  - form.inputs :for =\> [:inquiries, @inquiries] do |inquiry\_form|  
= inquiry\_form.input :text, :hint =\> t(“inquiryinfo”)

### PROBLEM

- the form renders fine for the customer and inquiry fields --\> OK!
- the form renders the hidden field only for the FIRST machine in my  
@machines collection (and there is definitly more then 1 machine in  
the collection, I already checked this with array size and by  
outputting the machines in a for each loop)

### QUESTION

- Did I miss some configuration?
- are nested forms not usable for habtm associations???  
–\> if so, how could I handle my “use case” in the controller?

I’d be sooo happy I any of you could help me out with this…I’m  
really lost ☹  
P.S. Sorry for my English, it’s not my native tongue 🙂
