nathanvda/cocoon — Issue #34: Update collection in _fields partial
Author: link-er
Created: 2012-01-20 11:44:19+00:00
State: open

Hello
I'm trying to implement dinamyc changes in collection in select input of partial for nested object.
I'm using cocoon (1.0.15), formtastic (2.0.2), rails (3.1.3).
I have model Contact:

class Contact < ActiveRecord::Base
has_many :infos, :as => :informable
accepts_nested_attributes_for :infos, :reject_if => proc {|attributes| attributes[:key].blank? || attributes[:value].blank? }, :allow_destroy => true
end

I have model Info:

class Info < ActiveRecord::Base
belongs_to :informable, :polymorphic => true
belongs_to :key
end

And I have model Key:

class Key < ActiveRecord::Base
has_many :infos
end

Here is partial _info_fields.html.erb:

<%= f.inputs do %>
<%= f.input :key, :as => :select, :include_blank => false, :collection => Key.all %>
<%= f.input :value %>
<%= link_to_remove_association "remove", f, :id => "remove_info" %>
<% end %>

And I have dialog, that can be called from the parent form directly and user can CRUD Key model from it. And I want collection in input :key on partial to update after these actions, but newly added partial contains old collection.
How can I get updated collection on newly added partials?