问题
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.
E.g.:
form_for @team do |t|
f.fields_for :players |p|
p.radio_button :is_captain, "is_captain_group"
Where is_captain is a boolean field on the player model. The above method does not work...
EDIT: 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.
回答1:
Update:
Then you have to declare a virtual attribute (e.g :captain) on the team model:
On the view:
form_for @team do |t|
f.fields_for :players |p|
t.radio_button :captain, p.id
On team.rb:
def captain=(id)
self.players.each do |player|
player.is_captain = (player.id == id)
end
end
来源:https://stackoverflow.com/questions/6334231/rails-3-radiobutton-group-in-fields-for