Rails 3 - 通过嵌套属性保存少数模型的模型

时间:2012-04-04 13:31:17

标签: ruby-on-rails ruby ruby-on-rails-3 model nested-attributes

我的模型很少 - 用户教师 TeacherLeader

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

当我试图保存我的模型时,我会收到奇怪的错误,例如“用户登录不能为空”等。我知道用户模型的验证会生成它们(我不知道为什么,即使在 params 中有值,我也会遇到这样的错误)。我想,我做错了,因为提交后我有 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> 

1 个答案:

答案 0 :(得分:1)

你不需要

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

但是类似

<%= tl.select( :teacher_id, 
                        options_for_select( @teachers_collection, @choosen_teacher )) %>