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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

Q&A

解決済

2回答

615閲覧

rails 複数のコントローラーから一つのモデルを操作する場合

Normin

総合スコア15

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

0グッド

0クリップ

投稿2022/08/30 09:06

編集2022/08/31 04:10

前提

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

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

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

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

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

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

winterboum

2022/08/30 09:20

エラーメッセージはそれだけではないと思うので省略せずに全部載せてください
guest

回答2

0

自己解決

自己解決しました。

interviews_controller.rb

1 def update 2 @applicant = Applicant.find(params[:interview_id]) 3 @applicant.update!(params) 4 end

としたらうまくいきました。

参考:https://ichigick.com/record-not-found-without-id/

PATCHにpath名はないため自分で設定しなければいけなくなってしまったことも沼った要因だと思われます。
解決に協力していただいたwinterboumさんありがとうございました。

投稿2022/08/31 05:20

Normin

総合スコア15

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

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

0

標準的な rouytes.rb でないので、rails routes で調べてください
interviews/1/interviews/update に対してどういう名前がつけられているか
例えば
user_password PATCH /users/password(.:format) passwords#update
と有った場合は user_password がその名前です。
form_with model: @applicant, url:'interviews/update',
のところを
form_with model: @applicant, url: 名前_path(id: @applicant)
としてみてください

投稿2022/08/31 02:05

winterboum

総合スコア23347

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

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

Normin

2022/08/31 04:12

回答していただきありがとうございます。おそらくupdateにはPATCHを使うのかなと思いroutes.rbのpatch "interviews/update"の行に, as: :interviews_updateを追記しurl:interview_interviews_update_path(id: @applicant)と変えたのですが違うエラーが出てしまいました。自分でも何が原因が考えてみます。
Normin

2022/08/31 04:23 編集

urlを@applicant.idにしたらview画面の表示はできるようになったのですが以下のようなエラーが出てしまいました Started PATCH "/interviews/4/interviews/update" for ::1 at 2022-08-31 13:13:53 +0900 Processing by InterviewsController#update as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "applicant"=>{"family_name"=>"hope", "given_name"=>"huge", "gender"=>"1", "selection_economic"=>"0", "selection_abroad"=>"0", "document_nyukainegai"=>"0", "document_sankousyorui"=>"0", "document_juminhyo"=>"0", "document_shikaku"=>"0", "document_keizai"=>"0", "receptionist"=>"fad"}, "commit"=>"Update 入会希望者", "interview_id"=>"4"} Completed 404 Not Found in 2ms (ActiveRecord: 0.0ms | Allocations: 753) ActiveRecord::RecordNotFound (Couldn't find Applicant without an ID): app/controllers/interviews_controller.rb:11:in `update' Started GET "/interviews/4/edit" for ::1 at 2022-08-31 13:13:53 +0900 Processing by InterviewsController#edit as HTML Parameters: {"id"=>"4"} Applicant Load (0.2ms) SELECT "applicants".* FROM "applicants" WHERE "applicants"."id" = ? LIMIT ? [["id", 4], ["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: 5.3ms | Allocations: 3378) Rendered layout layouts/application.html.slim (Duration: 13.9ms | Allocations: 8868) Completed 200 OK in 16ms (Views: 14.4ms | ActiveRecord: 0.2ms | Allocations: 9678)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問