fukasawamoe / yoriyoi

1 stars 0 forks source link

スケジュールのタスクの時間が勝手に入ってしまう #111

Closed fukasawamoe closed 1 month ago

fukasawamoe commented 1 month ago

スケジュール作成ページにてスケジュールのタスクの時間を入力しない場合にバリデーションが発生せず00:00の表記となってしまいます。 本来は、バリデーションメッセージと共にblank(まだ設定をしていない状態)にしたいです。 そして、バリデーションメッセージのタイミングを全て同じにしたいです。 (現状 時間のバリデーションが先に一つ表示されるのみで、日付を満たすとその他のバリデーションエラーメッセージが出てくるようになっています) Image from Gyazo

デバッグにて確認を行うと、どうやらtask_timeがblankの際には00:00が登録されてしまうのがデフォルトのようでした。

From: /Users/fukasawamoe/workspace/RUNTEQ/PF/生活/yoriyoi/app/controllers/schedules_controller.rb:41 SchedulesController#create:

    36: 
    37:     # 新しいパラメータとしてマージ
    38:     merged_params = schedule_params.merge(tasks_attributes: sorted_tasks_hash)
    39: 
    40:     @schedule = current_user.schedules.build(merged_params)
 => 41: binding.pry
    42:     if @schedule.save
    43:       flash[:notice] = 'スケジュールを作成しました'
    44:       redirect_to schedule_path(@schedule)
    45:     else
    46:       flash.now[:alert] = 'スケジュールの作成に失敗しました。'

[1] pry(#<SchedulesController>)> schedule_params
=> #<ActionController::Parameters {"name"=>"", "tasks_attributes"=>#<ActionController::Parameters {"0"=>#<ActionController::Parameters {"task_time(1i)"=>"1", "task_time(2i)"=>"1", "task_time(3i)"=>"1", "task_time(4i)"=>"", "task_time(5i)"=>"", "to_do"=>"", "memo"=>"", "_destroy"=>"false"} permitted: true>} permitted: true>} permitted: true>
[2] pry(#<SchedulesController>)> @schedule.tasks
=> [#<Task:0x000000010ac34cf8
  id: nil,
  schedule_id: nil,
  task_time: Mon, 01 Jan 0001 00:00:00.000000000 LMT +09:18,
  to_do: "",
  memo: "",
  position: nil,
  created_at: nil,
  updated_at: nil>]

そのため、saveを行う前のパラメータの時点でチェックを行うようにしました (追記部分です の部分です)

  def create
    # tasks_attributes をハッシュから配列に変換
    tasks_attributes_array = params[:schedule][:tasks_attributes].values

----!!!追記部分です!!!----↓↓
    tasks_attributes_array.each do |task_attributes|
      if task_attributes["task_time(4i)"].blank? || task_attributes["task_time(5i)"].blank?
        flash.now[:alert] = 'タスクを行う時間を入力してください'
        render :new, status: :unprocessable_entity
        return
      end
    end
----!!!追記部分です!!!----↑↑

    # 配列をソート
    sorted_tasks_attributes = tasks_attributes_array.sort_by do |task_attributes|
      task_time_hour = task_attributes["task_time(4i)"].to_i
      task_time_minute = task_attributes["task_time(5i)"].to_i
      task_time_hour * 60 + task_time_minute
    end

    # ソートされた配列を再度ハッシュ形式に変換
    sorted_tasks_hash = sorted_tasks_attributes.each_with_index.map { |attrs, index| [index.to_s, attrs] }.to_h

    # 新しいパラメータとしてマージ
    merged_params = schedule_params.merge(tasks_attributes: sorted_tasks_hash)

    @schedule = current_user.schedules.build(merged_params)
binding.pry
    if @schedule.save
      flash[:notice] = 'スケジュールを作成しました'
      redirect_to schedule_path(@schedule)
    else
      flash.now[:alert] = 'スケジュールの作成に失敗しました。'
      render :new, status: :unprocessable_entity
    end
  end

そうすることで時間のエラーメッセージは出ますが、やはり00:00の表記になってしまいます Image from Gyazo

解決の糸口が見つからないです....

kenchasonakai commented 1 month ago

バリデーションで頑張らなくてもHTML側のrequired属性でやりたいこと出来るかもなので見てみてください https://web-designer.cman.jp/html_ref/attr/?a=required