如何在 rails 提交时设置多个默认表单值?

how to set multiple default form values on submission in rails?

attendance table 有属性 status, date,recuritment_idproject_site_id

project_siteattendance

one_to_many 关联

recuritmentattendance有one_to_many关联。

当属性 status 加入考勤#form 视图时,我正在从 recruitment table 获取属性。

<% (1..(Time.days_in_month @project_site.attendance_month.strftime("%m").to_i)).each do |date| %> 这里 @project_site.attendance_month 包含出勤月份值。根据月份,我从 recuritmnet table 计算天数列以及 name

这里是视图-


holydays master 包含相应的月份 holyday 的日期并在视图中自动匹配日期并打印“H”,
所有输入默认选择为 P。最终 submission_button 更改布尔属性。现在在最终提交时,我想将所有默认选择的 P 推入出席 table.


attendance_controller.rb
  def new
    if @project_site.submission_status == false
      @attendance = Attendance.new

      @date = HolydayCalendar.all

      @recruitment = Recruitment.all.where(current_status: "2")
    else
      redirect_to project_site_attendances_path
    end
  end

from.html.erb(考勤控制器视图)

<table>
   <thead>
      <tr>
         <th class="attendance-emp-name">Emp. Name</th>
         <% (1..(Time.days_in_month @project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
         <th class="text-center"><%= date %></th>
         <% end %>
      </tr>
   </thead>
   <tbody>
      <% @recruitment.where(location: @project_site.site_id).each do |recruitment| %>
      <tr>
         <td class="attendance-emp-name"><%= recruitment.name %></td>
         <% (1..(Time.days_in_month @project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
         <%= form_with(model: attendance, :html => {:id => 'attendance-form-validation'}, url:[@project_site, @attendance], local: true) do |f| %>
         <% if HolydayCalendar.find_by(date: (@project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s), total_site_id: @project_site.site_id)%>
         <td class="holyday text-center"><%= "H" %></td>
         <% elsif recruitment.attendances.find_by(attendance_date: (@project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) == nil %>
         <td>
            <%= f.select :status, [['P', 1], ['A', 2], ['L', 4], ['WE', 5], ['CO', 6]], {}, { onchange: 'this.form.submit()', class: 'attendance-select-input' } %>
         </td>
         <% else %>
         <% attendance_value =  recruitment.attendances.find_by(attendance_date: (@project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) %>
         <%if attendance_value.status == 1 %>
         <td class="presant text-center"><%="P" %></td>
         <% elsif attendance_value.status == 2 %>
         <td class="absent text-center"><%="A" %></td>
         <%elsif attendance_value.status == 3 %>
         <td class="holyday text-center"><%="H" %></td>
         <%elsif attendance_value.status == 4 %>
         <td class="leave text-center"><%= "L" %></td>
         <%elsif attendance_value.status == 5 %>
         <td class="weekend text-center"><%= "WE" %></td>
         <%elsif attendance_value.status == 6 %>
         <td class="compoff text-center"><%= "CO" %></td>
         <% end %>
         <% end %>
         <%= f.hidden_field :attendance_date, value: (@project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)%>
         <%=f.hidden_field :recruitment_id, value: recruitment.id%>
         <%=f.hidden_field :project_site_id, value: @project_site.id%>
         <% end %>
         <% end %>
      </tr>
      <% end %>
   </tbody>
</table>
<% if @project_site.submission_status == true %>
<div class="text-center">
   <%= link_to "Submit Attendance", set_submission_status_project_site_path(@project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary disabled" %>
</div>
<% else %>
<div class="text-center">
   <%= link_to "Submit Attendance", set_submission_status_project_site_path(@project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary" %>
</div>
<% end %>


project_sites_controller.rb

  def set_submission_status
      @project_site = ProjectSite.find(params[:id])
      @project_site.update(submission_status: true)

  end

有点不清楚你的问题是什么,但我认为它与你的表单发回数组操作选项有关。

如果你想让你的 html 表单成为 return 一个信息数组,那么你需要在你的 html 中使用 name = "attendance[]" 语法指明它,注意方括号。

查看有关该主题的这篇文章https://mattstauffer.com/blog/a-little-trick-for-grouping-fields-in-an-html-form/