它的代码如下:
<%= f.fields_for :availabilities do |ff| %>
<% ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].each do |day| %>
<div class="row">
<div class="col-md-4">
<div class="ck-button">
<label>
<%= ff.check_box "day", {multiple: true, checked: @therapist.available_times.key?(day)}, day, false %>
<span><%= day %></span>
</label>
</div>
</div>
<div class="col-md-4">
<div class="answer">
<%= ff.select "start_time", ["1 AM", "2 AM", "3 AM", "4 AM", "5 AM", "6 AM", "7 AM", "8 AM", "9 AM", "10 AM", "11 AM", "12 PM", "1 PM", "2 PM", "3 PM", "4 PM", "5 PM", "6 PM", "7 PM", "8 PM", "9 PM", "10 PM", "11 PM"], {:selected => (@therapist.available_times.key?(day) ? @therapist.available_times[day][:start] : '7' )} %>
</div>
</div>
<div class="col-md-4">
<div class="answer">
<%= ff.select "end_time", ["1 AM", "2 AM", "3 AM", "4 AM", "5 AM", "6 AM", "7 AM", "8 AM", "9 AM", "10 AM", "11 AM", "12 PM", "1 PM", "2 PM", "3 PM", "4 PM", "5 PM", "6 PM", "7 PM", "8 PM", "9 PM", "10 PM", "11 PM"], {:selected => (@therapist.available_times.key?(day) ? @therapist.available_times[day][:end] : '17' )} %>
</div>
</div>
</div>
<% end %>
但是,当我提交此消息时,发送到我的控制器的数据如下:
"availabilities"=>{"start_time"=>"1 AM", "end_time"=>"1 AM", "day"=>["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]}
我真正想要得到的是:
"availabilities"=>[{"start_time"=> "1 AM", "end_time"=>"1 AM", "day" => "Monday"},{"start_time"=> ...},...]
仅适用于所选日期。
从管制员的角度看,我的许可证如下:
:availabilities => [:day, :start_time, :end_time],
编辑:
我将multiple
更改为false
,现在可用性具有我想要的形式,但是我只得到了最后一个检查项,而不是所有检查字段及其相关时间的数组
答案 0 :(得分:1)
您必须按某种方式对字段名称进行分组。试试这个:
首先,使用each_with_index
进行一些索引以将它们分组:
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].each_with_indexx do |day, idx|
现在您可以编写如下内容:
<%= ff.check_box "availabilities[#{idx}][day]", .... %>
<%= ff.select "availabilitlies[#{idx}][start_time]", .... %>
<%= ff.select "availabilitlies[#{idx}][end_time]", .... %>
参数将有所不同,而不是对象数组:
availabilities => {'0' => {'day' => 'Sunday', "start_time" => .., "end_time" => ...}, '1' => {'day' => 'Monday', .... }}
答案 1 :(得分:1)
尝试以下代码:
<%= f.fields_for :availabilities do |ff| %>
<div class="row">
<div class="col-md-4">
<div class="ck-button">
<label>
<% days = ['Mon', 'Tue', 'Wen', 'Thu', 'Fri', 'Sat', 'Sun'] %>
<% days.map do |day| %>
<%= ff.check_box :count,{multiple: false},day,nil %>
<%= ff.label day %>
<%end%>
</label>
</div>
</div>
<div class="col-md-4">
<div class="answer">
<%= ff.select "start_time", ["1 AM", "2 AM", "3 AM", "4 AM", "5 AM", "6 AM", "7 AM", "8 AM", "9 AM", "10 AM", "11 AM", "12 PM", "1 PM", "2 PM", "3 PM", "4 PM", "5 PM", "6 PM", "7 PM", "8 PM", "9 PM", "10 PM", "11 PM"], {:selected => (@therapist.available_times.key?(day) ? @therapist.available_times[day][:start] : '7' )} %>
</div>
</div>
<div class="col-md-4">
<div class="answer">
<%= ff.select "end_time", ["1 AM", "2 AM", "3 AM", "4 AM", "5 AM", "6 AM", "7 AM", "8 AM", "9 AM", "10 AM", "11 AM", "12 PM", "1 PM", "2 PM", "3 PM", "4 PM", "5 PM", "6 PM", "7 PM", "8 PM", "9 PM", "10 PM", "11 PM"], {:selected => (@therapist.available_times.key?(day) ? @therapist.available_times[day][:end] : '17' )} %>
</div>
</div>
</div> <% end %>