質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

949閲覧

Couldn't find User with 'id'= が解決できません

zoff77

総合スコア19

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/10/31 05:05

タイムカードを申請するアプリを作成しています。
下記のカラムに値が入っている日付のみを更新したいのですが、
成功処理、失敗処理のどちらともCouldn't find User with 'id'が出てしまいます。
原因が分からないので、解決策を教えて頂けないでしょうか?
宜しくお願いします。

イメージ説明

カラム入力画面

*@userはbefore_actionにて@user = User.find(params[:id])を指定*

rb

1# 勤怠変更申請 2 3 4 5 def edit_one_month 6 @attendance = Attendance.find(params[:id]) 7 @superior = User.where(superior: true).where.not( id: current_user.id ) 8 end 9 10 def update_one_month 11 ActiveRecord::Base.transaction do 12 attendances_params.each do |id,item| 13 @attendance = Attendance.find(id) 14 if item[:indicater_check_change].present? && 15 item[:note].present? && 16 item[:started_at] && item[:finished_at].present? && item[:started_at] < item[:finished_at] 17 @attendance.update_attributes!(item) 18 flash[:success] = "勤怠変更を受け付けました" 19 redirect_to user_url(@user) 20 else 21 flash[:danger] = "無効な入力データがあった為、更新をキャンセルしました。" 22 redirect_to edit_one_month_user_attendance_url(@user, @attendance) 23 return 24 end 25 end 26 rescue ActiveRecord::RecordInvalid 27 flash[:danger] = "無効な入力データがあった為、更新をキャンセルしました。" 28 redirect_to edit_one_month_user_attendance_url(@user, @attendance) 29 return 30 end 31 end 32 33 34 35 36private 37 # 勤怠編集 38 def attendances_params 39 # userに紐ずくattendanceテーブルの(出勤,退勤,翌日,備考,指示者確認(どの上長か,指示者確認(申請かどうか)) 40 params.require(:user).permit(attendances: [:started_at, :finished_at, :tomorrow, :note, :indicater_check_change, :indicater_reply_change])[:attendances] 41 end 42 43

erb

1<%= form_with(model: @user, url: update_one_month_user_attendance_path(@user, @attendance), local: true, method: :patch) do |f| %> 2<div> 3 <h1>勤怠編集画面</h1> 4 <table class="table table-bordered table-condensed table-hover" id="table-attendances"> 5 <thead> 6 <tr> 7 <th>日付</th> 8 <th>曜日</th> 9 <th>出社</th> 10 <th>退社</th> 11 <th>翌日</th> 12 <th>在社時間</th> 13 <th>備考</th> 14 <th>指示者確認<div class="maru size_small black"> 15 <div class="letter3"></div> 16 </th> 17 </tr> 18 </thead> 19 <tbody> 20 <% @attendances.each do |day| %> 21 <%= f.fields_for "attendances[]", day do |attendance| %> 22 <% css_class = 23 case $days_of_the_week[day.worked_on.wday] 24 when '土' 25 'text-primary' 26 when '日' 27 'text-danger' 28 end 29 %> 30 <tr> 31 <td><%= l(day.worked_on, format: :short) %></td> 32 <td class="<%= css_class %>"><%= $days_of_the_week[day.worked_on.wday] %></td> 33 <td><%= attendance.time_select :started_at, {include_blank: true}, {class: "form-control bootstrap-date-only-width"} %></td> 34 <td><%= attendance.time_select :finished_at, {include_blank: true}, {class: "form-control bootstrap-date-only-width"} %></td> 35 <td><%= attendance.check_box :tomorrow,id: "tomorrow" %></td> 36 <td> 37 <% if day.started_at.present? && day.finished_at.present? %> 38 <%= str_times = working_times(day.started_at, day.finished_at) %> 39 <% @total_working_times = @total_working_times.to_f + str_times.to_f %> 40 <% end %> 41 </td> 42 <td><%= attendance.text_field :note, class: "form-control" %></td> 43 <td><%= attendance.collection_select(:indicater_check_change, @superior, :name, :name, {prompt: "上長を選択して下さい"}, {class: "form-control input-sm" })%></td> 44 <% end %> 45 <% end %> 46 </tbody> 47 </table> 48</div> 49 50<div class="center"> 51 <%= link_to "キャンセル", user_path(date: @first_day), class: "btn btn-lg btn-primary" %> 52 <%= f.submit "編集を保存する", class: "btn btn-lg btn-primary" %> 53</div> 54<%end%> 55 56

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

エラー画面の下の方に書いてにあるparamsをみるとuser_idが11、idが218のようです。

@user = User.find(params[:id])

では、params[:id]としているのでIDが218番のUserを探してしまっています。
おそらくここでほしいユーザーデータは以下のようにして取得できるかと思います。

@user = User.find_by(id: params[:user_id])

併せて、 @userはbefore_actionにて@user = User.find(params[:id])をedit_one_monthのときには行わないようにする必要もあるかと思います。

投稿2020/10/31 06:39

hatsu

総合スコア1809

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zoff77

2020/11/14 12:00

解決しました!ありがとうございます!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問