Rails 3,fields_for 中的单选按钮组? [英] Rails 3, radiobutton group in fields_for?
问题描述
我认为这应该有点微不足道,但不知何故我一定做错了什么..而且我找不到任何谷歌结果证明我有任何好处的事实,我可能遗漏了一些点.我的主题几乎说明了一切:
如何制作在 fields_for 迭代中生成的单选按钮组.
I think this should be kinda trivial, but somehow I must be doing something wrong.. And the fact that I can't find any google results proving me any good, I might be missing some point. My subject almost says it all:
How can I make a radiobutton group that is generated in a fields_for iteration.
例如:
form_for @team do |t|
f.fields_for :players |p|
p.radio_button :is_captain, "is_captain_group"
其中 is_captain 是玩家模型上的布尔字段.上面的方法不行...
Where is_captain is a boolean field on the player model. The above method does not work...
如下所示,应该共享"单选按钮组的是fields_for中列出的所有球员 - 例如11名球员,只能选择一名队长.
As posted below, it is the all the listed players in the fields_for that should "share" the radio button group - e.g 11 players, only one can be selected captain.
推荐答案
更新:
然后你必须在团队模型上声明一个虚拟属性(例如:captain):
Then you have to declare a virtual attribute (e.g :captain) on the team model:
在视图上:
form_for @team do |t|
f.fields_for :players |p|
t.radio_button :captain, p.id
在 team.rb 上:
On team.rb:
def captain=(id)
self.players.each do |player|
player.is_captain = (player.id == id)
end
end
这篇关于Rails 3,fields_for 中的单选按钮组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!