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.new1.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| %>
Parent Information
<!--<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 class="span3">
<%= student_parent.label :middle_name, :class =>
‘control-label’%>
<%= student_parent.text_field :middle_name %>
<div class="span3">
<%= student_parent.label :last_name, :class =>
‘control-label’,:required => true %>
<%= student_parent.text_field :last_name %>
<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 class="span3">
<%= student_parent.label :middle_name, :class =>
‘control-label’%>
<%= student_parent.text_field :middle_name %>
<div class="span3">
<%= student_parent.label :last_name, :class =>
‘control-label’,:required => true %>
<%= student_parent.text_field :last_name %>
</div>
<% end %>
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 P…