2

我有几个模型 - UserTeacherTeacherLeader

class User < ActiveRecord::Base                                                              
  attr_accessor   :password                                                              
  attr_accessible :user_login,                                                            
                  :password,
                  :teacher_attributes,
                  :teacher_leader_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中。所以,在用户模型中,我添加了accepts_nested_attributes_for :teacher_leaderand attr_accessible :teacher_leader_attributes

我也有控制器:

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

  def create
    user = User.new( params )
    user.user_role = "class_head"

    if user.save
      flash[:success] = "Successfully created class head!"
    else
      flash[:error] = user.errors.full_messages
    end
  end
end

我也有TeacherLeader控制器的视图(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 %>

  <div class="control-group">
    <%= f.label :user_login, "Login", :class => "control-label" %>
    <div class="controls">
      <%= f.text_field :user_login %>
    </div>
  </div>

  <div class="control-group">
    <%= f.label :password, "Pass", :class => "control-label" %>
    <div class="controls">
      <%= f.text_field :password %>
    </div>
  </div>  
  <% end %>

<%= f.submit "Create", :class => "btn btn-large btn-success" %>

当我试图保存我的模型时,我得到了一些奇怪的错误,比如“用户登录不能为空”等等。我知道 User 模型的验证会生成它们(我不知道为什么,即使在params中有值我也会得到这样的错误)。我想,我认为我做错了什么,因为提交后我有这样的参数

teacher_id: '1'
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  user_login: schoolh_1rF32
  password: txaqxuTXz96auhX
commit: Create
action: create
controller: teacher_leaders 

但我应该有这样的东西:

user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      user_login: schoolh_1rF32
      password: txaqxuTXz96auhX
      teacher_leader_attributes: 
        teacher_id: '1'

怎么了?我可以解决吗?

UPD:类头创建页面的 HTML 代码:

<form accept-charset="UTF-8" action="http://0.0.0.0:3000/teacher_leaders" class="form-horizontal" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="ZKlDTKG8SU8fZuMrUfQoCOSGknOhj651DT2LJDrfliA=" /></div>
    <fieldset>

          <div class="control-group">
            <label class="control-label" for="user_teacher_leader_attributes_teacher_id">Teacher names</label>
            <div class="controls">

            <select id="teacher_id" name="teacher_id">
              <option value="1" selected="selected">Jack P. Tompson </option>
              <option value="2">Ronald V. Herwud</option>        
            </div>
          </div>

      <div class="control-group">
        <label class="control-label" for="user_user_login">Login</label>
        <div class="controls">
          <input id="user_user_login" name="user[user_login]" size="30" type="text" />
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="user_password">Password</label>
        <div class="controls">
          <input id="user_password" name="user[password]" size="30" type="text" />
        </div>
      </div>  
</fieldset> 
    <input class="btn btn-large btn-success" name="commit" type="submit" value="Create" />
</form> 
4

1 回答 1

1

你不需要

<%= select_tag( :teacher_id, 
                        options_for_select( @teachers_collection, @choosen_teacher )) %>

但类似的东西

<%= tl.select( :teacher_id, 
                        options_for_select( @teachers_collection, @choosen_teacher )) %>
于 2012-04-04T14:24:06.983 回答