0

我有几个模型 - UserTeacherTeacherLeader

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 %>   

问题是这里的选择表格没有出现。为什么?我做错了什么吗?

4

1 回答 1

1
<%= f.fields_for :teacher_leader do |tl| %>
于 2012-04-04T11:56:01.927 回答