Forum: Ruby on Rails nested forms with select lists

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
anrake o. (Guest)
on 2009-05-31 08:38
(Received via mailing list)
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
nodoubtarockstar (Guest)
on 2009-06-01 11:15
(Received via mailing list)
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.

anrake o. (Guest)
on 2009-06-01 18:24
(Received via mailing list)
Thanks. Somewhat edited, it basically looks like this.


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? }}

VIEW: part of edit.html.erb
<% cpform.fields_for :tags do |tag_form| %>
  <%='tag', 'tag_group', [["value_1","value1"],
["value_2","value2"], ["value_3","value3"]]) %>
  <%='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 %>


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

  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
      render :action => 'edit'

On Jun 1, 1:53 pm, nodoubtarockstar <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.