通过引用,我在ParticipantAttendance,TeamAttendance表中存储具有多对多关系的值。该表包含Participant表和Event表的引用。
我使用ParticipantAttendance的Index页面和TeamAttendance来存储值。
路线档案:
Rails.application.routes.draw do
resources :team_results do
collection do
put :result
end
end
resources :participant_results do
collection do
put :result
end
end
resources :team_attendances do
collection do
put :attendance
end
end
resources :participant_attendances do
collection do
put :attendance
end
end
resources :groupinfos
resources :events
resources :participants
get 'results/index'
get 'attendance/index'
root "participants#index"
end
在设置participant_result,team_results和参与者,事件之间的关系之前。出勤模块自动存储值,但现在它没有按照预期存储值。
event.rb:
class Event < ActiveRecord::Base
has_many :selections
has_many :participants, through: :selections
has_many :groupevents
has_many :groupinfos, through: :groupevents
has_many :participant_attendances
has_many :participants, through: :participant_attendances
has_many :participant_results
has_many :participants, through: :participant_results
has_many :team_attendances
has_many :groupinfos, through: :team_attendances
has_many :team_results
has_many :groupinfos, through: :team_results
validates :name, presence: true
end
participant.rb:
class Participant < ActiveRecord::Base
has_many :selections
has_many :events, through: :selections
has_many :groupdetails
has_many :groupinfos, through: :groupdetails
has_many :participant_attendances
has_many :events, through: :participant_attendances
has_many :participant_results
has_many :events, through: :participant_results
validates :name, presence: true
validates :email,:email => true
validates :phone,presence: true,
numericality: true,
length: { :minimum => 10, :maximum => 10 }
validates :college, presence: true
def full_info
"#{name}, Participant ID: #{id} "
end
end
participant_attendance.rb:
class ParticipantAttendance < ActiveRecord::Base
belongs_to :participant
belongs_to :event
end
participant_attendances / index.html.erb:
<p id="notice"><%= notice %></p>
<h1>Listing Participant Attendances</h1>
<%= form_tag attendance_participant_attendances_path, method: :put do %>
Round 1
<%= radio_button_tag "round", 1 %>|
Round 2
<%= radio_button_tag "round", 2 %>|
Round 3
<%= radio_button_tag "round", 3 %>|
Round 4
<%= radio_button_tag "round", 4 %>|
Round 5
<%= radio_button_tag "round", 5 %><br><br>
Present :
<%= radio_button_tag "mark_present", "present" %>|
Absent :
<%= radio_button_tag "mark_present", "absent" %><br><br>
<%= submit_tag "Mark Attendance" %>
<table>
<thead>
<tr>
<th></th>
<th>Participant</th>
<th>Events</th>
<th>Round</th>
<th>Status</th>
<th colspan="5"></th>
</tr>
</thead>
<tbody>
<% @participant_attendances.each do |participant_attendance| %>
<tr>
<td><%= check_box_tag "p_ids[]", participant_attendance.id %></td>
<td><%= participant_attendance.participant.name %></td>
<td><%= participant_attendance.event.name %></td>
<td><%= participant_attendance.round %></td>
<td><%= participant_attendance.status %></td>
<td><%= link_to 'Show', participant_attendance %></td>
<td><%= link_to 'Edit', edit_participant_attendance_path(participant_attendance) %></td>
<td><%= link_to 'Destroy', participant_attendance, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
ParticipantAttendancesController文件方法:
def attendance
params[:p_ids]
if params[:mark_present]=="present"
ParticipantAttendance.where(id: params[:p_ids]).update_all(status: 'Present', round: params[:round])
else
ParticipantAttendance.where(id: params[:p_ids]).update_all(status: 'Absent', round: params[:round])
end
redirect_to participant_attendances_url
end
有什么问题?
答案 0 :(得分:0)
只有在第一次提交表单重定向后刷新页面时,该表单才起作用。我发现提交按钮没有将值提交给控制器。
,但是刷新页面后,它运行正常。
我禁用了turbolinks
的此表单提交功能,并且有效。