Forum: Ruby on Rails nested forms with select lists

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
039351fc126e8ff2cb63d820b7ee7e72?d=identicon&s=25 anrake o. (anrake)
on 2009-05-31 06: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
4956b715c43829ef0b41ad52e606a6a8?d=identicon&s=25 nodoubtarockstar (Guest)
on 2009-06-01 09: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.

Cheers!
039351fc126e8ff2cb63d820b7ee7e72?d=identicon&s=25 anrake o. (anrake)
on 2009-06-01 16:24
(Received via mailing list)
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 <jenniferwendl...@gmail.com>
This topic is locked and can not be replied to.