Rails 3, radiobutton group in fields_for?

♀尐吖头ヾ 提交于 2019-12-24 01:12:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!