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

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

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

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

Ruby on Rails

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

Q&A

0回答

1026閲覧

Couldn't find Card with 'id'=@card の対処の仕方を教えて下さい

kaoru_tujimiya

総合スコア36

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2018/06/26 05:25

編集2018/06/28 01:04

データの一覧表示、追加、編集、削除などができる基本的なページを作成してます。
その中で、追加したデータの編集を行って更新したところ以下のエラーが発生しました。

ActiveRecord::RecordNotFound in CardsController#edit
Couldn't find Card with 'id'=@card

調べてみたところ、idを引き渡して情報を取得しようとしたがidがnillになっている為、このエラーが発生したという感じでしょうか?
対処方法が分からないので助言宜しくお願いします。

controllerのページ
def edit
@card = Card.find(params[:id]) ←この部分が対象となってはいますが。
if request.patch? then
@card.update(card_params)
goback
end
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
追記
Cards_controllerのコードです。

class CardsController < ApplicationController
layout 'cards'

def index @cards = Card.all end def show @card = Card.find(params[:id]) end def add if request.post? then Card.create(card_params) goback else @card = Card.new end end def edit @card = Card.find(params[:id]) if request.patch? then @card.update(card_params) goback end end def delete Card.find(params[:id]).destroy goback end private def card_params params.require(:card).permit(:title, :author, :price, :publisher, :memo) end private def goback redirect_to '/cards' end

end

>>>>>>>>>>>>>>>>>>>>>>>>
追記
edit.html.erb のコードです。

<h1>Cards#edit</h1> <p><%= @card.title %>の内容を編集します。</p> <table> <%= form_for(@card,url:{controller:'cards',action: 'edit',id:'@card.id'}) do |form| %>
<tr> <th>タイトル</th> <td><%= form.text_field :title, { size:40 } %></td> </tr> <tr> <th>著者</th> <td><%= form.text_field :author, { size:40 } %></td> </tr> <tr> <th>価格</th> <td><%= form.number_field :price, { size:10 } %></td> </tr> <tr> <th>出版社</th> <td><%= form.text_field :publisher, { size:40 } %></td> </tr> <tr> <th>コメント</th> <td><%= form.text_area :memo, {cols:40, rows:10} %></td> </tr> <tr> <th></th> <td><%= form.submit "更新" %></td> </tr> <% end %>
</table> <p class="link"><a href="/cards">&lt;&lt;</a></p>

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2018/06/26 06:21

editアクションのroutesを見せてもらえますか?$ rails routesで確認できます。
kaoru_tujimiya

2018/06/26 07:00 編集

こちらでよろしいでしょうか?すみません宜しくお願いします。 Prefix Verb URI Pattern Controller#Action cards_index GET /cards/index(.:format) cards#index cards GET /cards(.:format) cards#index cards_show GET /cards/show(.:format) cards#show cards_add GET /cards/add(.:format) cards#add POST /cards/add(.:format) cards#add GET /cards/:id(.:format) cards#show GET /cards/edit/:id(.:format) cards#edit PATCH /cards/edit/:id(.:format) cards#edit GET /cards/delete/:id(.:format) cards#delete people GET /people(.:format) people#index people_add GET /people/add(.:format) people#add POST /people/add(.:format) people#create GET /people/:id(.:format) people#show GET /people/edit/:id(.:format) people#edit POST /people/edit/:id(.:format) people#update PATCH /people/edit/:id(.:format) people#update GET /people/delete/:id(.:format) people#delete dengonban GET /dengonban(.:format) dengonban#index POST /dengonban(.:format) dengonban#index dengonban_index GET /dengonban/index(.:format) dengonban#index POST /dengonban/index(.:format) dengonban#index takuma_index GET /takuma/index(.:format) takuma#index takuma GET /takuma(.:format) takuma#index takuma_other GET /takuma/other(.:format) takuma#other POST /takuma(.:format) takuma#index POST /takuma/index(.:format) takuma#index rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
kaoru_tujimiya

2018/06/26 07:02

上から5行目あたりまでが対象の部分かと思われます
退会済みユーザー

退会済みユーザー

2018/06/26 07:31 編集

card_controllerも見せてもらえますか?追記の形で質問文に乗せていただきたいです。
退会済みユーザー

退会済みユーザー

2018/06/26 07:34 編集

card_controllerはeditのデータが送信されるupdateの部分です。見当たらないので...。
退会済みユーザー

退会済みユーザー

2018/06/27 01:40

updateの記述はeditの中にありましたね、すみません。editアクションの中でアップデートしようとしてるみたいに見るんですがそれは可能なんでしょうか?(試みたことがない)。通常編集した内容はPOSTでupdateアクションに送信され、そのアクションでアップデートします。前コメントのcontrollerの部分は必要ありませんでした、申し訳ありません。代わりにedit内でアップデートできる仕組みみたいなのがわからないのでviewでどんな書き方をしてるのかが気になります。HTMLファイルを読み込んだ後でページ移行することなくアップデートしようとしてるように見えるのですが、JavaScriptでも使っているのでしょうか?
kaoru_tujimiya

2018/06/28 01:05

ご指摘ありがとうございます。viewのeditの部分を追加しました。JavaScriptは使っていないです。すみません宜しくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問