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.
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 %>
</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
What version of Rails are you using? In the meantime, you should
research
scope in models…
Hi Elizabeth, thanks for the reply. I want to take attendance for entire
class of students not just one person.
I am using Rails 4.2.1 and ruby 2.2.1p85 (2015-02-26 revision 49769)
[x86_64-darwin14]
you can use sunil_custom_field gem.
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| %>
<td ><%= builder.text_field(:attendance_date) %></td>
<td ><%= builder.label :present, "Present?" %><%=
builder.check_box :present %>
</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