I feel more comfortable in deferring to my more advanced colleagues here. Pretty sure that this should work..... But at an elegance level I am not happy with this. Seems to me that in the Attendance model I should use a scope.
<%= form_for @students, :url=> {:controller=>"student", :action => "list"} do |nf| %> <% for student in @students %> <table > <tr > <th >First Name</th> <th >MI</th> <th >Last Name</th> </tr> <tr > <td ><%= nf.text_field(student.first_name) %></td> <td ><%= nf.text_field(student.mi) %></td> <td ><%= nf.text_field(student.last_name) %></td> </tr> <%= nf.fields_for :attendances, student.find(student.id).attendences do |builder| %> ### Not sure that .find is necessary in Rails 4. I think you could just write ### <%= nf.fields_for :attendances, student(student.id).attendances do |builder| %> <tr > <td ><%= builder.text_field(:attendance_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> <br /> <div style="display:none> <%= nf.hidden_field(:id, :value => student.id) %> </div> <% end %> <% end %> Student Controller def list if params[:commit] == "Add Attendence Record" if add ...... end else @students = Student.find(:all) end end def add @students_tmp = Student.find(params[:id]) @students_tmp.attendances << Attendance.new() if @students_tmp.errors.empty? @students = Student.find(:all) 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 Attendance Model belongs_to :student ## Posed question ## scope :match_student_parent , where(:student_id => :id) ## The tie between the controller and model, I am not certain... Assuming attributes accessible Then fields_for in the view should read: <%= nf.fields_for :match_student_parent, student.attendances.match_student_parent do |builder| %> Pretty sure advanced colleagues should understand the spirit in which I am writing On Monday, June 22, 2015 at 2:23:09 PM UTC-4, Mohammad Akram wrote: > > 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.... >> >> On Monday, June 22, 2015 at 2:04:04 PM UTC-4, Mohammad Akram wrote: >>> >>> 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 >>>> >>>> >>>> >>>> On Monday, June 22, 2015 at 8:10:15 AM UTC-4, Mohammad Akram wrote: >>>>> >>>>> 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. >>>>> >>>> -- 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+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/23c2a61b-941e-4ab6-81ce-9b7ab0bd7d36%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.