开发者

has_many :through and FormBuilder.fields_for

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, an开发者_开发技巧d 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

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜