has_many :through 和 FormBuilder.fields_for

发布于 2024-08-24 13:02:34 字数 528 浏览 9 评论 0 原文

我有一个类 Bar ,它有一个用户定义的配置键和值列表,定义如下:

class Bar < ActiveRecord::Base

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

end

因此可用的配置键来自 Foo 类以及它们来自 Bar 类。

我正在为此 Bar 类创建一个表单,并且需要使用 name 属性作为标签来循环 config_keys 中的每个字段,但文本框应该是对于 config_valuesvalue

我所看到的是,如果我这样做,

我认为集合上的 f.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, 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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-08-31 13:02:34

我最终让它工作,但关键是不要使用 f.fields_for bar.config_keys ...相反,我确保每个键都存在一个值记录(在我的模型上的 before_save 上),我这样做而是值集合的嵌套形式。

不过,我仍然不确定为什么表单生成器的对象是一个数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文