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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

354閲覧

paramsにidが渡せていない、または文字列が入ってしまっているエラー

milktea246

総合スコア23

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2022/05/11 14:47

編集2022/05/12 10:59

前提

rails初学者です。
ペットの詳細画面と編集画面を作成した後、現在時間を表示させていと思い、index.html.erbにコードを記述していたのですが、詳細画面と編集画面に遷移しようとするとエラーが発生してしまいました。
数時間前は詳細画面と編集画面に遷移できており、詳細画面と編集画面のコードは触っていないので他のところのエラーだとおもうのですが、分からず止まってしまっています。
createアクションは正常に動いており、DBにペット情報を登録できています。

また、今回の件とは関係がないかもしれませんが編集画面にて更新ボタンを押すとcreateアクションが動いてしまって更新したはずなのにDBに新たな情報が登録されてしまうという挙動が起きていました。

実現したいこと

詳細画面と編集画面を表示できるようにしたいです。

発生している問題・エラーメッセージ

https://gyazo.com/31d3350122137527ed45a27df3272529
Couldn't find Information with 'id'=edit
'id'=editという文字列が入ってしまっていると思われます。
(編集画面に遷移しようとすると'id'=showとなり、同じような現象に陥っています。)

該当のソースコード

informationコントローラー

class InformationsController < ApplicationController def index @informations = Information.all end def new @information = Information.new end def create @information = Information.new(information_params) @information.user_id = current_user.id if @information.save redirect_to information_path(@information) else render :new end end def show @information = Information.find(params[:id]) end def edit @information = Information.find(params[:id]) end def update @information = Information.find(params[:id]) @information.update(information_params) redirect_to information_path(@information) end private def information_params params.require(:information).permit(:name, :sex, :kinds, :color, :birthday, :pet_image) end end

show.html.erb

<section class="hero is-info"> <div class="hero-body"> <div class="container"> <h1 class="title"> ペット詳細 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-5"> <article class="panel is-link"> <p class="panel-heading"> <%= @information.user.nickname %>さんのペット </p> <div class="panel-block"> <p class="control"> <%= @information.user.profile %> </p> </div> <%= link_to user_path(@information.user), class: "panel-block" do %> <%= @information.user.nickname %> さんのページへ <% end %> </article> </div> </div> </div> </div> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-7"> <div class="card"> <div class="card-image"> <figure class="image is-4by5"> <img class="is-rounded"><%= attachment_image_tag @information, :pet_image %> </figure> </div> <div class="card-content"> <div class="media"> <div class="media-content"> </div> </div> <div class="content"> <table class = "table is-striped"> <tbody> <tr> <th>ペット情報</th> </tr> <tr> <td><u>名前:<%= @information.name %></u></td> </tr> <tr> <td><u>性別:<%= @information.sex %></u></td> </tr> <tr> <td><u>種類:<%= @information.kinds %></u></td> </tr> <tr> <td><u>色:<%= @information.color %></u></td> </tr> <tr> <td><u>誕生日:<%= @information.birthday %></u></td> </tr> </body> </table> <br> <br> <% if @information.user.id == current_user.id %> <%= link_to "編集画面へ", edit_information_path(@information), class: "button is-info" %> <% end %> </div> </div> </div> </div> </div> </div> </section>

edit.html.erb

<section class="hero is-warning"> <div class="hero-body"> <div class="container"> <h1 class="title"> ペット情報編集 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-6"> <%= form_for @information, url: informations_path, method: :post, local: true do |f| %> <%= f.label :name, "ペットの名前", class: "label" %> <%= f.text_field :name, class: "input" %> <%= f.label :sex, "ペットの性別", class: "label" %> <%= f.select :sex, {'♂': 1, '♀': 2}, { include_blank: '-'}, { class: 'select-is-primary' , required: true } %> <%= f.label :kinds, "ペットの種類", class: "label" %> <%= f.text_field :kinds, class: "input" %> <%= f.label :color, "ペットの色", class: "label" %> <%= f.text_field :color, class: "input" %> <%= f.label :birthday, "ペットの誕生日", class: "label" %> <%= raw sprintf( f.date_select( :birthday, class:"select-is-primary", id:"birth-date", use_month_numbers: true, prompt:'-', start_year: 1950, end_year: (Time.now.year + 10), date_separator: '%s'), "<p> 年 </p>", "<p> 月 </p>") + "<p> 日 </p>" %> <br> <br> <%= f.label :pet_image, "ペットの写真", class: "label" %> <%= f.attachment_field :pet_image, class: "input" %> <br> <br> <%= f.submit "更新", class: "button is-primary" %> <% end %> </div> </div> </div> </section>

index.html.erb

<h1>index</h1> <%= @informations.each do |information| %> <%= attachment_image_tag information, :pet_image %> <%= information.user.nickname %> <%= information.updated_at.strftime("%Y-%m-%d %H:%M") %> <% end %>

試したこと

・index.html.erbで現在時間を表示させようとした後に起こってしまったのでindex.html.erbの時間表示に関する記述を消してみましたが全く関係ありませんでした。

原因がわかる方がいましたら、ご教授ください。
よろしくお願いします。

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

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

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

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

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

winterboum

2022/05/11 16:40

Viewも載せましょう
milktea246

2022/05/11 16:48

失礼致しました。 Viewを載せましたのでご確認いただけますと幸いです。
winterboum

2022/05/11 17:06

indexからの遷移でおかしいのですから indexを載せなくては
milktea246

2022/05/11 17:19

度々すいません、ご確認宜しくお願いします。 また、application.rbに config.time_zone = 'Tokyo' ↑こちらを追加しました。
winterboum

2022/05/11 23:53

index.html.erbにコードを記述していたのですが、詳細画面と編集画面に遷移 とあるのに、indexにはその遷移部分が見当たりませんが?
milktea246

2022/05/12 00:39 編集

説明足らずですみません。まだ詳細画面と編集画面を先に作ってしまっていてリンクはまだ作っていないのでindex.html.erbやnew.html.erbからurlに直接入力して遷移していました。
milktea246

2022/05/12 01:53

winterboum様、今回の件ですがすごく単純なことでした。 私が直接URLを入力する際にDBに登録されている一つ目のデータの詳細画面を表示する際にinformations/1と入力しなければいけなかったところ、informations/showと入力してしまっていることが原因でした。 (編集画面についても✖️informations/edit → ◯informations/1/edit で表示されました) 色々ご指摘していただきありがとうございました。基礎ができていないのでもっと復習しようと思います。
guest

回答1

0

自己解決

原因

index.html.erbで編集画面と詳細画面へのリンクを記述していなかったが為に直接遷移するためのURLを入力していました。
その際にDBに登録されている一つ目のデータの詳細画面を表示する際にinformations/1と入力しなければいけなかったところ、informations/showと入力してしまっていることが原因でした。
(編集画面についても✖️informations/edit → ◯informations/1/edit で表示されました)

投稿2022/05/12 01:59

milktea246

総合スコア23

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問