我是 Rails 新手,从最近两天开始就陷入了这个问题我有一个 student 和 student_parent 模型,他们有一对多的关系我想通过一个对象从 form 到 datase 建立数据库属性。
控制器方法
def new
@student = Student.new
1.times{ @student.student_parents.build }
.....
.....
end
创建方法:-
def create
@student = Student.new(params[:student])
respond_to do |format|
if @student.save
format.html { redirect_to Student, notice: 'Student was successfully created.' }
format.json { render json: @student, status: :created, location: @student }
else
format.html { render action: "new" }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
模型:-
class Student < ActiveRecord::Base
include ErrorMessages
belongs_to :user
has_many :student_parents
attr_accessible :birth_date, :blood_group, :first_name, :gender, :last_name, :middle_name,
:school_name, :student_rollno, :email, :user_id, :student_parents_attributes
accepts_nested_attributes_for :student_parents
end
形式:-
<%= simple_form_for @student, :html => { :class => 'form-horizontal' } do |f| %>
<div class="row-fluid">
<div class="span3">
<%= f.label :first_name, :class => 'control-label',:required => true %>
<%= f.text_field :first_name %>
</div>
<div class="span3">
<%= f.label :middle_name, :class => 'control-label'%>
<%= f.text_field :middle_name %>
</div>
<div class="span3">
<%= f.label :last_name, :class => 'control-label',:required => true %>
<%= f.text_field :last_name %>
</div>
</div>
<div class="control-group">
<label class = "control-label"> Email </label>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<label class = "control-label"> Birth Date <abbr title="required">*</abbr></label>
<div class="controls">
<%= f.text_field :birth_date, :class => 'text_field' ,'data-behaviour' => 'datepicker' %>
</div>
</div>
<% model_class = StudentParent %>
<div class="page-header">
<h4> Parent Information</h4>
</div>
<%= f.fields_for :student_parents do |student_parent| %>
<div class="row-fluid">
<!--<div class="span9">-->
<h5> Father Name </h5>
<div class="span3">
<%= student_parent.label :first_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :first_name %>
</div>
<div class="span3">
<%= student_parent.label :middle_name, :class => 'control-label'%>
<%= student_parent.text_field :middle_name %>
</div>
<div class="span3">
<%= student_parent.label :last_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :last_name %>
</div>
</div>
<div class="row-fluid">
<h5> Mother Name </h5>
<div class="span3">
<%= student_parent.label :first_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :first_name %>
</div>
<div class="span3">
<%= student_parent.label :middle_name, :class => 'control-label'%>
<%= student_parent.text_field :middle_name %>
</div>
<div class="span3">
<%= student_parent.label :last_name, :class => 'control-label',:required => true %>
<%= student_parent.text_field :last_name %>
</div>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
students_path, :class => 'btn' %>
</div>
提交时仅在学生表中构建学生信息,在学生父表中构建母亲信息。但它错过了父信息。