Ruby on rails 3.2 以从关联模型实例派生的新形式创建默认值

Ruby on rails 3.2 以从关联模型实例派生的新形式创建默认值,ruby-on-rails-3.2,simple-form,model-associations,Ruby On Rails 3.2,Simple Form,Model Associations,我希望在创建新记录时,使用simple_form和存储在关联模型中的数据填充表单字段 class Game < ActiveRecord:Base has_many :events attr_accessible :name, :number_of_players end class Event < ActiveRecord:Base belongs_to :game attr_accessible :name, :number_of_players end cl

我希望在创建新记录时,使用simple_form和存储在关联模型中的数据填充表单字段

class Game < ActiveRecord:Base
  has_many :events
  attr_accessible :name, :number_of_players
end

class Event < ActiveRecord:Base
  belongs_to :game
  attr_accessible :name, :number_of_players
end
class游戏
不同的比赛需要不同数量的球员(棒球9人,篮球5人,足球11人)

在我的表单中,当创建一个事件时,我有一个关联下拉列表,用户在其中选择游戏。我想为玩家数量预先填充一个事件字段,如果需要,用户可以覆盖该字段。例如,basketball将使用5填充该字段,但用户可能希望将其更改为3,因为该特定事件可能是3对3游戏。我不想修改游戏数据

从本质上讲,我试图基于在同一表单中选择的关联模型实例的值为表单创建默认值。我尝试过很多方法,但都没有成功

那么:

<%= f.input :number_of_players, :input_html => { :value => (number_of_players_value(f.object)} %>
此解决方案提供一个事件的默认关联游戏。
如果用户选择其他游戏时,需要更改“玩家数量”字段的内容,而不是您需要一些JavaScript来实现这一点。

我忘记响应您的anwwer,这确实为我所寻找的行为提供了一种新的方法。谢谢
def number_of_players_value(event)
  event.new_record? ? event.game.number_of_players : event.number_of_players
end