したいこと
time_cardsテーブルとusersテーブルがあり、
time_card:userは対:1の関係です。
time_cardのフォームにuserを選択するセレクトボックスを設けて、
その値によってtime_cardのuser_idを登録しようとしています。
ruby
1【view】 2<%= form_with model: @time_card, url: apply_update_url do |form| %> 3<%= form.text_field :business_outline %> 4<%= form.select :overtime_application_target, User.where(superior: true).where.not(id: current_user.id).collect{ |p| [ p.name, p ] } %> 5<%= form.submit "登録する", :class => "btn btn-primary" %> 6<% end %>
ruby
1【controller】 2def apply_update 3 @time_card = TimeCard.find(params[:time_card][:id]) 4 if @time_card.update_attributes!(time_card_params) 5 flash[:success] = "登録が完了しました。" 6 redirect_to controller: 'time_cards', action: 'show', user_id: current_user.id 7 else 8 render 'edit' 9 end 10 end
問題
ただこのコードだと、以下のエラーが表示されます。
ruby
1*** ActiveRecord::AssociationTypeMismatch Exception: User(#70031622812880) expected, got "#<User:0x00007f6305751a88>" which is an instance of String(#19701280)
自分で考えたこと
外部キーの値入力は、数字ではなくインスタンスを入れなければTypeMismatch Exceptionが吐かれると記事で見たので、セレクトボックスのvalueにインスタンスを設定しているのですが、結局TypeMismatch Exceptionが吐かれてしまっています。
原因はセレクトボックスのvalueに設定していることで、インスタンスではなく文字列として扱われてしまっているのかなと考えています。
ただその解決方法がわかりません。
ご教授いただけませんでしょうか、よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/05 16:05 編集
2019/02/05 22:21
2019/02/09 03:51
2019/02/09 06:31
2019/02/11 01:28
2019/02/11 02:07 編集
2019/02/11 02:08