multiple attendance entry in rails database with nested forms? -
i using ruby v 2.0.0p195 , rails v 3.2.13
i new rails , ruby , have dived in there learn how getting interesting project.
i want create entry form school attendance record list student names , provide opportunity update records simultaneously given date. i.e 1 big button on form updates selected date students @ once while giving opportunity mark particular students absent.
i using existing student controller methods create individual records students , works fine. can create individual attendance register each student , works well, when attempt put entire group in 1 list not able work , lost how proceed.
do create new method in student controller using nested form approach or use attendance controller methods aren't being used create , edit these records. if use attendance controller how list students , link these records within attendance controller?
i have 2 tables:
student , attendance
student.rb
has_many :attendances, :dependent => :destroy accepts_nested_attributes_for :attendances, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attendance.rb
attr_accessible :present, :attdate, :absent, :partial, :notified, :notes, :student_id belongs_to :student, :class_name => 'student'
the routes.rb looks this:
root to: 'home_page#home' 'attendance/:id', to: 'students#attentry' 'attendance', to: 'students#attentry' resources :attendances resources :students member 'attentry' end end resources :students resources :attendances end resources :students 'students/attentry'
i think of routes getting confused have had add bits individual things working.
the controller method using student attendance this:
def attentry @student = student.find(params[:id]) @attendance = @student.attendances.build respond_to |format| format.html { render action: "attentry" } format.json { render json: @student } end end
and view this:
<%= form_for (@student) |f| %> <%= f.text_field :name -%> <div class="span9"> <table class = "table table-striped"> <tr> <td>date</td> <td>present</td> <td>absent</td> <td> part day</td> <td>notified</td> <td>notes</td> </tr> <%= f.fields_for :attendances |fc| %> <tr> <td><%= fc.text_field :attdate %></td> <td><%= fc.check_box :present %></td> <td><%= fc.check_box :absent %></td> <td><%= fc.text_field :partial %></td> <td><%= fc.check_box :notified %></td> <td><%= fc.text_area :notes, :rows => 3 %></td> </tr> <% end %> </div> </table> <br /> <div class="actions"> <%= f.submit %> </div>
anyway, table shows attendance details 1 student , attempting students on 1 table can update them in 1 go button , check boxes.
is more javascript thing or can while remaining in ruby/rails land?
i have been going through lot of railscasts , stack overflow posts , have been gradually putting , patching things together, part has had me stumped weeks. beginning wonder if version of ruby , rails giving me headaches lot of have found potential answers don't seem work when created verbatim rails casts etc......thanks in advance ideas, suggestions, places look.
the code assisted me in achieving multiple update form in attendance controller , helped me through problem:
def multiplenew if params["student_ids"].nil? @attendance = attendance.create(params[:attendance]) else params["student_ids"].each |id| @student = student.find(id.to_i) @attendance = @student.attendances.create(params[:attendance]) end end redirect_to attendances_url end
basically gathers id info checkboxes located next each student , creates daily attendance record each 1 ticked. there number of default values fields works nice , smoothly. set validations date not repeated particular student made in model.
it's using little of multiple edit concept railscasts fantastic ryan bates, massive amount of time on stack overflow, lot of reading of api , rails guides , little voodoo, code section helped me create multiple attendance records 1 submit button press , bunch of checkboxes. allows creating blank days in case of days no attendance required. got javascript in there well. 1 happy camper! onto getting change multiple edit page.......
Comments
Post a Comment