How can i add multiple records to a single table, through single form

53 views
Skip to first unread message

Mohammad Akram

unread,
Jun 22, 2015, 8:10:15 AM6/22/15
to rubyonra...@googlegroups.com
Hi, i have just started with rails. I am making a attendance web app. I want to insert student attendance into attendances table and unable to do it as the form just inserts only last entry of the form. Student table and Attendances table have associations(has_many,belongs_to,). Please let me know how the form should be and controller api should look like.
Message has been deleted

Mohammad Akram

unread,
Jun 22, 2015, 2:04:04 PM6/22/15
to rubyonra...@googlegroups.com
Hi Elizabeth, thanks for the reply. I want to take attendance for entire class of students not just one person.




On Monday, 22 June 2015 19:47:57 UTC+5:30, Elizabeth McGurty wrote:
Hope this helps... There might be typos, but I think it may lead you in the right direction... Not sure the most elegant direction 

Just assuming that attendance model contains attendance_date, present, comment

Otherwise checkout Railscasts:#196 Nested Model Form (revised). 

<%= form_for @student , :url=>  {:controller=>"student", :action => "list", :id => @student.id} do |nf| %>
  
        <table >
            <tr >
                <th >First Name</th>
                <th >MI</th>
                <th >Last Name</th>

            </tr>

            <tr >
                <td ><%= nf.text_field(:first_name)  %></td>
                <td ><%= nf.text_field(:mi) %></td>
                <td ><%= nf.text_field(:last_name) %></td>

               
            </tr>


              <%= nf.fields_for :attendances, @student.attendences do |builder| %>
       
            <tr >
              
                <td ><%= builder.text_field(:attendence_date)  %></td>
                <td ><%= builder.label :present, "Present?" %><%= builder.check_box :present %></td>
                <td ><%= builder.text_field(:comment)  %></td>
               
            </tr>
       
            <% end %>
              
            <tr >
            <td colspan=3 ><%= nf.submit "Add Attendence Record" %></td>
            </tr>
                   
        </table>
       

<% end %>

Student Controller

   def list
         
   if params[:commit]  == "Add Attendence Record"
        if add
    ......
    end
    else
      @student = Student.find(param[:id])
    end 
     
   end


def add

        @student = Student.find(params[:id])
        @student.attendences << Attendance.new()
        if @student.errors.empty?
             return true
     else
         return false       
         end

end

Student Model

   has_many :attendances, :dependent => :destroy
   attr_accessible :attendances_attributes, :allow_destroy => true
   accepts_nested_attributes_for :attendances
 

Attendence Model

  belongs_to :student
Message has been deleted

Mohammad Akram

unread,
Jun 22, 2015, 2:23:09 PM6/22/15
to rubyonra...@googlegroups.com
I am using Rails 4.2.1 and ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]



On Monday, 22 June 2015 23:46:37 UTC+5:30, Elizabeth McGurty wrote:
What version of Rails are you using?  In the meantime, you should research scope in models....
Message has been deleted

Sunil Kumar

unread,
Jul 6, 2015, 9:34:05 AM7/6/15
to rubyonra...@googlegroups.com
you can use sunil_custom_field gem.
Reply all
Reply to author
Forward
0 new messages