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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

457閲覧

ActiveRecord::RecordNotFound in KarutesController#edit Couldn't find Karute with 'id'=6のエラー

noe.black

総合スコア2

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/18 23:59

devise,cancancan,rails_adminを利用して、管理者のみがユーザーに紐づくカルテを作成、編集などができるように実装を進めています。

editアクションを実行しようとしたところ、

ActiveRecord::RecordNotFound in KarutesController#edit Couldn't find Karute with 'id'=6 Extracted source (around line #22): def edit @karute = Karute.find(params[:id])←色がついているのがここ end

とのエラーがでます。
一人では解決できないと思い、相談させていただきます。お力を貸していただければありがたいです。

###該当のソースコード

karutes_controller.rb class KarutesController < ApplicationController def index @users = User.where.not(id: current_user.id) end def new @karute = Karute.new end def create @karute = Karute.new(karute_params) @karute.user_id = params[:user_id] if @karute.save redirect_to user_path(@karute.user_id) else render :new end end def edit @karute = Karute.find(params[:id]) end def update @karute = Karute.find(params[:id]) if @karute.save redirect_to user_path(@karute.user_id) else render :edit end end private def karute_params params.require(:karute).permit(:pain, :illness, :history, :medicine, :habit, :memo, :date) end end
users_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) end end
karute.rb class Karute < ApplicationRecord belongs_to :user, optional: true end
user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable with_options presence: true do validates :password, format: { with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,100}+\z/i } validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]/ } validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]/ } validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/ } validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/ } validates :birth_date_id validates :phone_number, format: { with: /\A\d{11}\z/ } end has_one :address has_one :karute end
show.html.erb <h1>カルテ</h1> <%= link_to '一覧に戻る', root_path %> <% unless @user.karute.present?%> <%= link_to 'カルテを追加', new_user_karute_path(@user.id), method: :get %> <% else %> <%= link_to 'カルテを編集', edit_karute_path(@user.id), method: :get %> <h2 class="page-heading"><%= "#{@user.family_name}#{@user.first_name}さんのカルテ"%></h2> 省略 <% end %>
edit.html.erb h2 class="karute-title">カルテ編集</h2> <%= form_with model: @karute, url: karute_path, local: true do |f| %> <%= render 'shared/error_messages', model: f.object %> <div class="field"> <%= f.label "作成日" %><br /> <%= f.text_field :date %> </div> 省略 <div class="actions"> <%= f.submit "カルテ作成", class: :form__btn %> <%=link_to 'もどる', root_path, class:"back-btn" %> </div> <% end %>
routes.rb Rails.application.routes.draw do mount RailsAdmin::Engine => '/admin', as: 'rails_admin' devise_for :users, controllers: { registrations: 'users/registrations' } devise_scope :user do get 'addresses', to: 'users/registrations#new_address' post 'addresses', to: 'users/registrations#create_address' end root to: "karutes#index" resources :users, only: [:show] do resources :karutes, shallow: true end end

###rails routes

root GET / karutes#index user_karutes GET /users/:user_id/karutes(.:format) karutes#index POST /users/:user_id/karutes(.:format) karutes#create new_user_karute GET /users/:user_id/karutes/new(.:format) karutes#new edit_karute GET /karutes/:id/edit(.:format) karutes#edit karute GET /karutes/:id(.:format) karutes#show PATCH /karutes/:id(.:format) karutes#update PUT /karutes/:id(.:format) karutes#update DELETE /karutes/:id(.:format) karutes#destroy user GET /users/:id(.:format) users#show

###試したこと
https://railsguides.jp/routing.htmlより、ルーティングのネストをしていたので、
shallow: true
を付け浅いネストにしました。
URLの
http://localhost:3000/karutes/6/edit
の6については、カルテのidではなく、ユーザーのidで、カルテのuser_idと結び付けられれば解決するのでは?と思い試行錯誤していましたが、うまく行きません。

ご指摘をいただけると嬉しいです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Carte のような気が、、、
さておき
link_to 'カルテを編集', edit_karute_path(@user.id)
カルテのidでなくuserのIDが渡っています。
link_to 'カルテを編集', edit_karute_path(@user.karute.id)

投稿2021/01/19 04:14

winterboum

総合スコア23401

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

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

noe.black

2021/01/19 04:18

winterboum様ありがとうございます。 修正しましたら、idが渡るようになりました。 コントローラーの方ばかり見ていたので、ビューにミスがあることが自分的には盲点でした。 これからは、全体を見て、ミスがないか確認していこうと思います。 本当にありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問