User、Teacher、およびTeacherLeaderのモデルはほとんどありません。
class User < ActiveRecord::Base
attr_accessible ...,
:teacher_attributes
has_one :teacher
has_one :teacher_leader
accepts_nested_attributes_for :teacher_leader
end
class Teacher < ActiveRecord::Base
belongs_to :user
has_one :teacher_leader
end
class TeacherLeader < ActiveRecord::Base
belongs_to :user
belongs_to :teacher
end
ネストされた属性を介してTeacherLeaderを埋めたいと思います。だから、私はコントローラでそのようなことをします:
class TeacherLeadersController < ApplicationController
...
def new
@user = User.new
@teacher_leader = @user.build_teacher_leader
@teachers_collection = Teacher.all.collect do |t|
[ "#{t.teacher_last_name} #{t.teacher_first_name} #{t.teacher_middle_name}", t.id ]
end
@choosen_teacher = @teachers_collection.first.last unless @teachers_collection.empty?
end
end
また、そのようなビュー(new.html.erb)もあります:
<%= form_for @user, :url => teacher_leaders_url, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
<% f.fields_for :teacher_leader do |tl| %>
<div class="control-group">
<%= tl.label :teacher_id, "Teacher names", :class => "control-label" %>
<div class="controls">
<%= select_tag( :teacher_id,
options_for_select( @teachers_collection, @choosen_teacher )) %>
</div>
</div>
<% end %>
<% end %>
...
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
問題は、ここで選択フォームが表示されないことです。なんで?私は何か間違ったことをしていますか?