# How to create text\_field for has\_many form

**URL:** <https://www.ruby-forum.com/t/how-to-create-text-field-for-has-many-form/136199>\
**Category:** Rails\
**Created:** [April 29, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/how-to-create-text-field-for-has-many-form/136199 "2008-04-29T19:28:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [April 29, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/how-to-create-text-field-for-has-many-form/136199/1 "2008-04-29T19:28:05Z")

</div>

Hello

2 solid days of time and I can’t figure this/find answer in previous  
group post/find answer via google anything that works. I know the  
problem is me (and I will know doubt kick myself later) but if anyone  
could direct me I’d appreciate it.

I have items (called ‘WeeeItems’) in one place that need to be  
delivered somewhere else. When they get delivered we need a ‘transfer  
note’, (called ‘WeeeTransfer’). So, I need to create a new transfer  
note recording the destination,date etc and select items from a list  
of existing stock that are included with that transfer.

I’ve got forms and things working to add items into the  
‘WeeeItems’ (our stock waiting to transfer), but I can’t figure how to  
include given weeeitems with a specific transfer. Trying to keep  
things as simple as possible while I figure what I’ve got wrong  
(normally helps!) I’m finding I don’t know how to refer to the  
weee\_items\_id of the related table. As such, the view detailed below  
will NOT add anything to the transfer table failing with:

NoMethodError in WeeeController#confirmtransfer  
You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.save

So, I think this means I need a form field that references 1 or more  
WeeeItems, I just don’t know how to do that. I thought along the  
lines of  
f.text\_field :weeeitemsid  
but that and variations haven’t worked so far.

Any direction greatly appreciated.

Thanks,  
Steve

## Model

class WeeeTransfer \< ActiveRecord::Base  
has\_many :weee\_items  
#belongs\_to is in the other model  
end

## Controller

def confirmtransfer  
@transfer = WeeeTransfer.new(params[:transfer])  
if request.post? and @weeetype.save  
flash[:notice] = ‘WEEE Transfer added’  
redirect\_to :action =\> ‘index’  
end  
end

```
#this bit will get us all the UNtransfered weee items
#i will eventually use this to have list of check boxes for items

```

included in the transfer  
#but it’s not used yet… is that my problem?  
@tableweees = WeeeItem.find(:all, :include =\> “weee\_type”,  
:conditions =\> “weee\_transfers\_id is null”)

## View

# WEEE Items

```
<% form_for :transfer do |f| %>
Weee Destination: <%= f.text_field :weeetransferdestination %><br /

```

> 

```
Transfer Type: <%= f.text_field :weeetransfertype %><br />
Transfer Date: <%= f.text_field :weeetransferdate %><br />
  <%= submit_tag %>

<% end %>
```

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [April 29, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/how-to-create-text-field-for-has-many-form/136199/2 "2008-04-29T19:31:04Z")

</div>

you could add a hidden tag in there with the id of the weeItem
