views:

138

answers:

1

I have a class Bar that has a user-defined list of config keys and values, defined like this:

class Bar < ActiveRecord::Base

   has_many :config_keys, :through => Foo
   has_many :config_values

end

So the available config keys come from the Foo class and the values for those come from the Bar class.

I'm creating a form for this Bar class, and I need to loop over each of the fields in config_keys using the name property as the label, but the textbox should be for the value of the config_values

What I'm seeing is that if I do

<% f.fields_for bar.config_keys do |builder| %> <%= builder.object.class.to_s %> <-- Outputs 'Array' (what?) <% end %>

I thought that f.fields_for on a collection would do the looping for me.

Am I approaching this the right way? Feels like I'm really fighting the framework.

A: 

I ended up getting this to work, but the key was don't use f.fields_for bar.config_keys... instead I make sure that a value record exists for each of the keys (on a before_save on my model) and I do the nested form for the values collection instead.

I'm still not sure why that form builder's object was an array, though.

Ben Scheirman