Ruby on Rails Monday, June 22, 2015

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+unsubscribe@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/a8a5e638-67b8-4bef-8967-16011660b8dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment