I am new to rails, And stuck in this problem from last two days I have a student and student_parent model they have a one-to-many relationship i want to build database attribute through one object from form to datase.
controller’s method
def new
@student = Student.new
1.times{ @student.student_parents.build }
.....
.....
end
Create Method:-
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
Model:- class Student < ActiveRecord::Base 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
form:-
<%= 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>
when submit it only build student information in student table and mother information in student_parent table. But it miss the parent information.
I want to submit student entry in student table & Father and mother entry simultaneously in student_parents table.
Thanks, Tushar Patil.