前提
rails初学者です。
ペットの情報を登録する機能を作ろうと思い、フォームに必要な項目を入力し、登録するボタンを押しましたが、元の入力画面に戻ってしまい保存することができません。
実現したいこと
informationsコントローラーのcreateアクションを動かしてペット情報がDBに保存され、詳細画面に遷移するようにしたい。
発生している問題・エラーメッセージ
Started POST "/informations" for ::1 at 2022-05-10 09:22:54 +0900 Processing by InformationsController#create as HTML Parameters: {"authenticity_token"=>"hlOo1AP/QawmuxWMa2ewkWdKZmnrTyc13jszewpPQLPbm1qvK7zLMuV9SwXmnNJ2UIlHc+WwgoyPTM+rhW6eBw==", "information"=>{"name"=>"おこげ", "sex"=>"♂", "kinds"=>"雑種", "color"=>"黒", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007f8197a68f68 @tempfile=#<Tempfile:/var/folders/_x/593m4_t55hl77x0d_zdv5clm0000gn/T/RackMultipart20220510-75599-o6hkih.png>, @original_filename="IMG_5179.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"information[image]\"; filename=\"IMG_5179.png\"\r\nContent-Type: image/png\r\n">}, "year"=>"2011", "month"=>"6", "day"=>"2", "commit"=>"登録"} User Load (8.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/informations_controller.rb:11:in `create' Rendering informations/new.html.erb within layouts/application Rendered informations/new.html.erb within layouts/application (Duration: 4.2ms | Allocations: 1342) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 104ms (Views: 37.5ms | ActiveRecord: 8.2ms | Allocations: 34744)
データベースの画像
https://gyazo.com/8abd95118be38d18ea65a880a365483b
該当のソースコード
informationsコントローラー
class InformationsController < ApplicationController def index end def new @information = Information.new end def create @information = current_user.informations.build(information_params) if @information.save redirect_to information_path(@information) else render :new end end private def information_params params.require(:information).permit(:name, :sex, :kinds, :color, :birthday_id, :image) end end
新規登録画面
<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.text_field :sex, class: "input" %> <%= 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_id, "ペットの誕生日", class: "label" %> <span class="select is-primary"> <select name="year"> <option value="">-</option> <option value="1920">1920</option> (長くなってしまうので一部割愛) <option value="2030">2030</option> </select>年 </span> <span class = "select is-primary"> <select name="month"> <option value="">-</option> <option value="1">1</option> (一部割愛) <option value="12">12</option> </select>月 </span> <span class = "select is-primary"> <select name="day"> <option value="">-</option> <option value="1">1</option> (一部割愛) <option value="31">31</option> </select>日 </span> <br> <br> <%= f.label :image, "ペットの写真", class: "label" %> <%= f.attachment_field :image, class: "input" %> <br> <br> <%= f.submit "登録", class: "button is-primary" %> <% end %>
information モデル
class Information < ApplicationRecord belongs_to :user, optional: true attachment :image with_options presence: true do validates :name validates :sex validates :color validates :birthday_id validates :user end end
マイグレーションファイル
class CreateInformation < ActiveRecord::Migration[6.0] def change create_table :information do |t| t.string :name, null: false t.string :sex, null: false t.string :kinds t.string :color, null: false t.date :birthday_id, null: false t.string :image_id t.references :user, null: false, foreign_key:true t.timestamps end end end
試したこと
createアクションでsave!として、挙動を確かめてみると
birthdayとimageに値が入ってないことがわかりました。
paramsに正しい値が入っていないみたいです。
https://gyazo.com/3187ff22a46c45f1f0eada9b3288aebb
https://gyazo.com/4949df78329bf7a6eacef8a8395115c0
カラム名なども確かめてみているのですが、原因がわかりません。
どなたかわかる方がいらっしゃいましたらご教授ください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/10 05:53
退会済みユーザー
2022/05/10 06:08
2022/05/10 10:06
2022/05/10 17:30
退会済みユーザー
2022/05/11 01:00