@seiyarick (seiya)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

rollback transactionの原因について

Q&A

Closed

解決したいこと

新規投稿機能で名前、画像、紹介文を入力し保存してユーザー詳細ページに遷移したいのですが保存ができません。
ターミナル内では以下のようになります。 rollback transactionという記述があり、調べるとデータが保存できる前に戻ってしまっているみたいでした。
考えられる原因を教えていただきたいです。
よろしくお願いします。

発生している問題・エラー

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"3JVQ4gD5mMZid1n2vQDLNTq28fcRHjzWkknxo/OqOohmN/tFQp7UzfimA6HkYih502PdKC4/1QjOPAvfVHjeLg==", "dish"=>{"dish_name"=>"エビチリ", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007faac0e610c8 @tempfile=#<Tempfile:/tmp/RackMultipart20220411-31809-n0g0q4.jpeg>, @original_filename="エヒ�゙チリ.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"dish[image]\"; filename=\"\xE3\x82\xA8\xE3\x83\x92\xE3\x82\x99\xE3\x83\x81\xE3\x83\xAA.jpeg\"\r\nContent-Type: image/jpeg\r\n">, "introduction"=>"エビチリです"}, "commit"=>"CookOPする"}
Unpermitted parameter: :image
   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.7ms

該当するソースコード

*/dishes_controller.rb

 def create
    @dish = Dish.new(dish_params)
    @dish.save
    redirect_to user_path(current_user.id)

  end

  private

  def dish_params
    params.require(:dish).permit(:dish_name, :introduction)

  end

*/models/dish.rb

class Dish < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_many :favorits, dependent: :destroy

  has_one_attached :image
end

*/CookOP/db/schema.rb

ActiveRecord::Schema.define(version: 2022_04_09_064638) do

  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.integer "record_id", null: false
    t.integer "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "comments", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "dish_id"
    t.integer "user_id"
    t.text "comment"
    t.integer "comment_count"
  end

  create_table "dishes", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.string "dish_name"
    t.text "introduction"
  end

  create_table "favorits", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.integer "dish_id"
    t.integer "favorits_count"
  end

  create_table "relationships", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "follower_id"
    t.integer "follow_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.text "profile"
    t.boolean "is_deleted", default: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

自分で試したこと

rails cでDish.allとして確認したところ何も表示されないので保存されていないというところまで理解できschemaを確認しましたが特に問題ないと感じました。
ご教授のほどよろしくお願いします。

0 likes

1Answer

Unpermitted parameter: :image

imageパラメータが許可されていないって怒られていますね。

params.require(:dish).permit(:dish_name, :introduction, :image)

としてみては?

0Like

Comments

  1. @seiyarick

    Questioner

    コメントありがとうございます。
    無事に解決できました!ありがとうございました。
    パラメータで必要なカラムが定義されていないことによるエラーだったんですね。
    今後気をつけて行きます。

Your answer might help someone💌