前提
勤怠アプリを制作中です。
ひと月分のスタッフのシフトを一括登録したいのですが
シフト日付のパラメータが全て「月末」になってしまい、入力値とずれてしまいます。
実現したいこと
ひと月分のスタッフのシフトを一括登録
日付の入力値とパラメーターを一致させたい。
発生している問題・エラーメッセージ
エラーメッセージはありません。
ひと月分のシフトの日付が全て月末として登録されてしまいます。
該当のソースコード
html.erb
1<%= form_with(model: @form, url: works_path, method: :post, local: true) do |f| %> 2 <table> 3 <%= f.fields_for :works do |w| %> 4 <% @next_month.each do |d| %> 5 <%= w.hidden_field :user_id, value: @user %> 6 <tr> 7 <td> 8 <%= w.date_field :shift, value: d %> 9 </td> 10 <td> 11 <%= w.select :worktype, Work.worktypes.keys.map {|k| [k,k]} %> 12 </td> 13 </tr> 14 <% end %> 15 <% end %> 16 <%= f.submit "送信" %> 17 </table> 18 <% end %>
Ruby
1#コントローラー 2 3def new 4 @form = Form::WorkCollection.new 5 now = Date.current 6 @next_month = (now.beginning_of_month.next_month..now.end_of_month.next_month).to_a 7 end 8 9def create 10 @form = Form::WorkCollection.new(work_collection_params) 11 @user = params[:user_id] 12 if @form.save 13 flash[:success] = 'シフトを登録しました' 14 redirect_to works_path 15 else 16 flash.now[:failed] = "シフトの登録に失敗しました" 17 render :index 18 end 19end 20 21def work_collection_params 22 params.require(:form_work_collection) 23 .permit(works_attributes: [:shift, :user_id, :worktype]) 24 end 25
Ruby
1#コレクション 2class Form::WorkCollection < Form::Base 3 FORM_COUNT = 31 #ここで、作成したい登録フォームの数を指定 4 attr_accessor :works 5 6 def initialize(attributes = {}) 7 super attributes 8 self.works = FORM_COUNT.times.map { Work.new() } unless self.works.present? 9 end 10 11 def works_attributes=(attributes) 12 self.works = attributes.map { |_, v| Work.new(v) } 13 end 14 15 def save 16 Work.transaction do 17 self.works.map do |work| 18 work.save! 19 end 20 end 21 end 22end
試したこと
タイムゾーンなどは設定済みです。
Viewでは正常に日付が入力されてます。
検証ツールで確認しましたが、こちらも値は問題なく表示されています。
html
1<input value="2023-01-04" type="date" name="form_work_collection[works_attributes][0][shift]" id="form_work_collection_works_attributes_0_shift">
上記のまま、登録ボタンを押すとパラメーターが以下のようになってしまいます。
parameters
1"form_work_collection"=> 2 {"works_attributes"=> 3 {"0"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 4 "1"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 5 "2"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 6 "3"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 7 "4"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 8 "5"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 9 "6"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 10 "7"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 11 "8"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 12 "9"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 13 "10"=>{"user_id"=>"", "shift"=>"2023-01-31", "worktype"=>"出勤予定"}, 14 ...
補足情報(FW/ツールのバージョンなど)
まだまだ勉強不足で、初歩的な内容かもしれませんが、わかる方いましたら、ご教示お願いします。
form ではどんなHTMLが生成されてるんでしょうか?
ありがとうございます!
以下の通りです。
よろしくお願いします!
-----------------------------------
<form action="/works" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="KL5V4tDHgdYWowzQVR+MZvREUVdqb28duVYdjBHB6S5xvsseekVvkfNKGFbtJo/dSNAG2SZE8eviOkWowDHoFA==">
<input type="submit" name="commit" value="送信" data-disable-with="送信">
<table>
<input type="hidden" name="form_work_collection[works_attributes][0][user_id]" id="form_work_collection_works_attributes_0_user_id">
<tbody>
<tr>
<td>
<input value="2023-01-01" type="date" name="form_work_collection[works_attributes][0][shift]" id="form_work_collection_works_attributes_0_shift">
</td>
<td>
<select name="form_work_collection[works_attributes][0][worktype]" id="form_work_collection_works_attributes_0_worktype"><option selected="selected" value="出勤予定">出勤予定</option>
<option value="公休">公休</option>
<option value="欠勤">欠勤</option>
<option value="有給">有給</option></select>
</td>
</tr>
<input type="hidden" name="form_work_collection[works_attributes][0][user_id]" id="form_work_collection_works_attributes_0_user_id">
<tr>
<td>
<input value="2023-01-02" type="date" name="form_work_collection[works_attributes][0][shift]" id="form_work_collection_works_attributes_0_shift">
</td>
<td>
<select name="form_work_collection[works_attributes][0][worktype]" id="form_work_collection_works_attributes_0_worktype"><option selected="selected" value="出勤予定">出勤予定</option>
<option value="公休">公休</option>
<option value="欠勤">欠勤</option>
<option value="有給">有給</option></select>
</td>
</tr>
<input type="hidden" name="form_work_collection[works_attributes][0][user_id]" id="form_work_collection_works_attributes_0_user_id">
<tr>
<td>
<input value="2023-01-03" type="date" name="form_work_collection[works_attributes][0][shift]" id="form_work_collection_works_attributes_0_shift">
---------------
