# Nested forms with select lists

**URL:** <https://www.ruby-forum.com/t/nested-forms-with-select-lists/169190>\
**Category:** Rails\
**Created:** [May 31, 2009, 6:38am UTC](https://www.ruby-forum.com/t/nested-forms-with-select-lists/169190 "2009-05-31T06:38:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anrake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anrake](https://www.ruby-forum.com/u/anrake)\
**Post date:** [May 31, 2009, 6:38am UTC](https://www.ruby-forum.com/t/nested-forms-with-select-lists/169190/1 "2009-05-31T06:38:28Z")

</div>

I’d like to use a drop down select field to add multiple categories to  
my model (has\_many relationship) using nested forms in Rails 2.3.2. I  
can get it to work fine with straight text fields, but no matter what  
I do with select elements I always end up with “undefined method  
`merge’ for #Array:0x2246420”

Does anyone have a working example of using nested forms with select?

Thank you

---

<div class="post-metadata">

**Author:** ![anrake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anrake](https://www.ruby-forum.com/u/anrake)\
**Post date:** [June 1, 2009, 9:15am UTC](https://www.ruby-forum.com/t/nested-forms-with-select-lists/169190/2 "2009-06-01T09:15:28Z")

</div>

Can you possibly post some sample code so that we can see how your  
select lists are defined and how your controller is attaching the  
categories to your object? It might be a fairly easy fix to point out.

Cheers!

---

<div class="post-metadata">

**Author:** ![anrake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anrake](https://www.ruby-forum.com/u/anrake)\
**Post date:** [June 1, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/nested-forms-with-select-lists/169190/3 "2009-06-01T16:24:35Z")

</div>

Thanks. Somewhat edited, it basically looks like this.

MODEL:

class Cpicture \< ActiveRecord::Base  
belongs\_to :historicsite  
has\_many :tags, :dependent =\> :destroy  
accepts\_nested\_attributes\_for :tags, :allow\_destroy =\> true,  
:reject\_if =\> proc { |attrs| attrs.all? { |k, v|  
v.blank? }}  
end

VIEW: part of edit.html.erb  
\<% cpform.fields\_for :tags do |tag\_form| %\>  
\<%= tag\_form.select(‘tag’, ‘tag\_group’, [[“value\_1”,“value1”],  
[“value\_2”,“value2”], [“value\_3”,“value3”]]) %\>  
\<%= tag\_form.select(‘tag’, ‘tag\_name’, [[“value\_1”,“value1”],  
[“value\_2”,“value2”], [“value\_3”,“value3”]]) %\>

\<% unless tag\_form.object.nil? || tag\_form.object.new\_record? %\>  
\<%= tag\_form.label ‘\_delete’, ‘Remove:’ %\>  
\<%= tag\_form.check\_box ‘\_delete’ %\>  
\<% end %\>  
\<% end %\>

CONTROLLER:

def edit  
@cpicture = Cpicture.find(params[:id])  
2.times {@cpicture.tags.build}  
end

def update  
@cpicture = Cpicture.find(params[:id])  
if @cpicture.update\_attributes(params[:cpicture])  
flash[:notice] = ‘Cpicture was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @cpicture  
else  
render :action =\> ‘edit’  
end  
end

On Jun 1, 1:53 pm, nodoubtarockstar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
