Nested forms with select lists


#1

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


#2

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!


#3

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