前提
rails初心者です
railsで面接内容をメモするアプリを作成しています。
面接を受ける人のテーブルがあって、そこの内容を受付の人がまず入力し、その後に面接官が追記するようなものを想定しています。
コントローラー
・entrances_controller.rb
・interview_controller.rb
モデル
・applicant.rb
実現したいこと
- 受付人コントローラー(entrances_controller.rb)で面接を受ける人のレコードをnew,createで作成し、名前や通し番号を入力する。
- 面接官コントローラー(einterview_controller.rb)で面接を受ける人のレコードをedit,updateで更新し、面接の評価を書く。
railsの同一名を使うという大原則を無視してモデル名と異なるコントローラーを作ってしまった為、話が厄介になっているのだと思います。今は受付人コントローラーと面接官コントローラーのみですが、同じモデルを操作するコントローラーを増やしたいと考えているので、あまり一つのモデルと一つのコントローラーにまとめ、コントローラーにメソッドを大量に盛り込むというのが気が進まないですがその方が楽ですかね。。。?
ここで質問するのが初めてなので足りない情報などがあったらすみません
発生している問題・エラーメッセージ
エラーメッセージ Started PATCH "/interviews/1/interviews/update" for ::1 at 2022-08-31 10:19:40 +0900 Processing by InterviewsController#update as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "applicant"=>{"family_name"=>"hoge", "given_name"=>"huga", "gender"=>"0", "selection_economic"=>"1", "selection_abroad"=>"0", "document_nyukainegai"=>"1", "document_sankousyorui"=>"1", "document_juminhyo"=>"0", "document_shikaku"=>"0", "document_keizai"=>"0", "receptionist"=>"naya"}, "commit"=>"Update 入会希望者", "interview_id"=>"1"} (0.1ms) SELECT sqlite_version(*) ↳ app/controllers/interviews_controller.rb:11:in `update' Completed 404 Not Found in 11ms (ActiveRecord: 0.9ms | Allocations: 2416) ActiveRecord::RecordNotFound (Couldn't find Applicant without an ID): app/controllers/interviews_controller.rb:11:in `update' Started GET "/interviews/1/edit" for ::1 at 2022-08-31 10:19:40 +0900 Processing by InterviewsController#edit as HTML Parameters: {"id"=>"1"} Applicant Load (0.2ms) SELECT "applicants".* FROM "applicants" WHERE "applicants"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/interviews_controller.rb:7:in `edit' Rendering layout layouts/application.html.slim Rendering interviews/edit.html.slim within layouts/application Rendered interviews/edit.html.slim within layouts/application (Duration: 6.4ms | Allocations: 3717) Rendered layout layouts/application.html.slim (Duration: 31.7ms | Allocations: 20857) Completed 200 OK in 41ms (Views: 33.1ms | ActiveRecord: 0.5ms | Allocations: 25900)
該当のソースコード
routes.rb
ruby
1Rails.application.routes.draw do 2 resources :entrances 3 resources :interviews 4 resources :interviews do 5 patch "interviews/update" => "interviews#update", as: :interviews_update 6 end 7end
interviews/edit.html.slim
slim
1= form_with model: @applicant, url:'interviews/update', local: true do |f| 2 .form_group 3 = f.label :family_name 4 = f.text_field :family_name, class: 'form-control', id: 'family_name' 5 = f.label :given_name 6 = f.text_field :given_name, class: 'form-control', id: 'given_name' 7 = f.label :gender, '男性' 8 = f.radio_button :gender, 0 9 = f.label :gender, '女性' 10 = f.radio_button :gender, 1 11 br 12 = f.check_box :selection_economic, {}, 1, 0 13 = f.label :selection_economic 14 br 15 = f.check_box :selection_abroad, {}, 1, 0 16 = f.label :selection_abroad 17 br 18 br 19 = f.check_box :document_nyukainegai, {}, 1, 0 20 = f.label :document_nyuryonegai 21 br 22 = f.check_box :document_sankousyorui, {}, 1, 0 23 = f.label :document_sankousyorui 24 br 25 = f.check_box :document_juminhyo, {}, 1, 0 26 = f.label :document_juminhyo 27 br 28 = f.check_box :document_shikaku, {}, 1, 0 29 = f.label :document_gakuseki 30 br 31 = f.check_box :document_keizai, {}, 1, 0 32 = f.label :document_keizai 33 br 34 35 = f.label :receptionist 36 = f.text_field :receptionist, class: 'form-control', id: 'receptionist' 37 = f.submit nil, class: 'btn btn-primary' 38 39...
interviews_controller.rb
ruby
1class InterviewsController < ApplicationController 2 def index 3 @applicants = Applicant.all 4 end 5 6 def edit 7 @applicant = Applicant.find(params[:id]) 8 end 9 10 def update 11 applicant = Applicant.find(params[:id]) 12 applicant.update!(params) 13 end 14end 15 16 17...
ruby 3.1.0p0
Rails 7.0.3.1
rails
1interviews GET /interviews(.:format) interviews#index 2 POST /interviews(.:format) interviews#create 3 new_interview GET /interviews/new(.:format) interviews#new 4 edit_interview GET /interviews/:id/edit(.:format) interviews#edit 5 interview GET /interviews/:id(.:format) interviews#show 6 PATCH /interviews/:id(.:format) interviews#update 7 PUT /interviews/:id(.:format) interviews#update 8 DELETE /interviews/:id(.:format) interviews#destroy 9 interview_interviews_update PATCH /interviews/:interview_id/interviews/update(.:format) interviews#update 10 GET /interviews(.:format) interviews#index 11 POST /interviews(.:format) interviews#create 12 GET /interviews/new(.:format) interviews#new 13 GET /interviews/:id/edit(.:format) interviews#edit 14 GET /interviews/:id(.:format) interviews#show 15 PATCH /interviews/:id(.:format) interviews#update 16 PUT /interviews/:id(.:format) interviews#update 17 DELETE /interviews/:id(.:format) interviews#destroy 18
url: interview_interviews_update_path(id: @applicant)の場合
Rendering interviews/edit.html.slim within layouts/application Rendered interviews/edit.html.slim within layouts/application (Duration: 7.4ms | Allocations: 9239) Rendered layout layouts/application.html.slim (Duration: 7.7ms | Allocations: 9333) Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.1ms | Allocations: 10695) ActionView::Template::Error (No route matches {:action=>"update", :controller=>"interviews", :id=>#<Applicant id: 4, family_name: "agf", given_name: "gaff", reception_number: 301, gender: 1, receptionist: "fad", created_at: "2022-08-30 07:21:20.242973000 +0000", updated_at: "2022-08-30 07:21:20.242973000 +0000", selection_economic: 0, selection_abroad: 0, document_nyuryonegai: 0, document_sankousyorui: 0, document_juminhyo: 0, document_gakuseki: 0, document_keizai: 0>}, missing required keys: [:interview_id] Did you mean? interview_interviews_update_url): 4: .nav.justify-contnet-end 5: = link_to '一覧', interviews_path, class: 'nav-link' 6: 7: = form_with model: @applicant, url:interview_interviews_update_path(id: @applicant), local: true do |f| 8: .form_group 9: = f.label :family_name 10: = f.text_field :family_name, class: 'form-control', id: 'family_name' app/views/interviews/edit.html.slim:7
回答2件
あなたの回答
tips
プレビュー