-1
值我有一个锻炼模式:嵌套形式与模型
class Workout < ActiveRecord::Base
attr_accessible :time
belongs_to :user
has_and_belongs_to_many :trainers
accepts_nested_attributes_for :trainers
end
而且一个教练模式:
class Trainer < ActiveRecord::Base
attr_accessible :name
validates_uniqueness_of :name
has_and_belongs_to_many :workouts
end
我需要有一个巢教练的形式,它允许拉从数据库值。 现在我有这样的一个新的锻炼形式内:
<%= f.fields_for :trainers do |builder| %>
<%= builder.select :trainer, options_for_select(Trainer.all.collect{ |u| [u.name, u.id] }) %>
<br>
<% end %>
我得到“未定义的方法'教练”为#”
我在做什么错?
您需要,否则实例化一个教练这是一个空的关系,所以在你的控制器:@ workout.trainers.build。另外,options_from_collection_for_select(Trainer.all,:id,:name) –
我已经在我的控制器中创建了代码。 – DarkSun
options_from_collection_for_select没有帮助。 – DarkSun