Tushar Patil
2013-Jul-29 05:34 UTC
How to use multiple models in view template form in rails
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| %> <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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAMFjjRcfULb%3DOzr%2BHr4XvJeXg0_z3J2vXekaKmUzDeB4CLYcdA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
bacrossland
2013-Jul-29 16:23 UTC
Re: How to use multiple models in view template form in rails
> I am new to rails, And stuck in this problem from last two days I have astudent and student_parent model they have a *one-to-many* relationship i want to *build database attribute through one object from form to datase*. Hi Tushar, Your controller does not have to only speak to the Student model. You could have it also create the StudentParent entries from the information you got submitted by the form. Once you verify that the Student object and the StudentParent object saved successfully you would return true. A failure in either you would return false with the error that caused the failure. Thanks, Bryan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2074c127-0872-4014-a578-37f022b52a75%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.