前提
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の時間表示に関する記述を消してみましたが全く関係ありませんでした。
原因がわかる方がいましたら、ご教授ください。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー