简体   繁体   English

在阵列导轨中存储Time_Select

[英]Storing Time_Select in Array Rails

I'm looping through an array of weekdays (sun-mon). 我正在遍历一系列工作日(星期日)。 I would like to save a time for each day in a separate array called delivery_times : 我想每天在一个单独的数组中保存时间,该数组称为delivery_times

views/orders/_form.html.erb 意见/命令/ _form.html.erb

  <% Date::DAYNAMES.each_with_index do |day, index| %>
      <div class="field">
          <%= day %><br>
          <%= check_box_tag "delivery_days[]", index %>
      </div>

      <div class="field">
          <%= f.label :time %><br>
          <%= f.time_select :time, "delivery_times[]" => :time %>
      </div>   
  <% end %>

Controller 调节器

  def create
    @times = params[:delivery_times]
    params[:delivery_days].each do |day, time|
      @order = Order.create(order_params.merge( {:day => day}, {:time => @times[index]} ))
    end
      redirect_to @order, notice: 'Order was successfully created.'
  end

Currently delivery_days is saved correctly, but delivery_times does not save the times. 当前delivery_days已正确保存,但delivery_times未保存时间。 I tried finding time_select_tag , but it does not exist. 我尝试找到time_select_tag ,但它不存在。 How can I store the time into the array? 如何将时间存储到数组中?

您可以使用ActionView time_tag文档

<%= f.time_field :time, "delivery_times" => :time %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM