Railsにてdeviseを使用したユーザー情報のアップデートを実行中エラーが出ており躓いています…
##エラー文
refistrations_controller.rbのupdateにbinding.pryでエラーを出力しております
**registrations_controller.rb** def update super binding.pry end
ターミナル上でエラー文を確認
[2] pry(#<Users::RegistrationsController>)> @user.update ArgumentError: wrong number of arguments (given 0, expected 1) from /Users/eleaco/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/persistence.rb:616:in `update'
よくみる引数が一つ必要だが足りないと言われています…
##コード
viewからみていきます
**registrations>edit.html.erb** <%= form_with model: @user, url: user_registration_path, local: true do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <div class="edit-profile-container"> <%= f.text_area :profile, class: 'edit-profile'%> </div>
registrarions_controller.rb
**registrations_controller.rb** class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_account_update_parameters, only: [:update] def update #@user.update(account_update_params)ここ消しました理由はbefore_actionで #configure_account_update_parametersメソッドを呼び出しているからです end protected def configure_account_update_parameters devise_parameter_sanitizer.permit(:account_update, keys: [:profile]) end
application_controller.rb
**application_controller.rb** class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname]) devise_parameter_sanitizer.permit(:account_update, keys: [:profile]) end end
##皆様のご指摘で追加したコード達
**registrations_controller.rb** def update super binding.pry end
binding.pryで中に何が入っているか確認します
[2] pry(#<Users::RegistrationsController>)> @user => #<User id: 2, nickname: "gest", email: "gest@gest", profile: "これは自己紹介です", created_at: "2020-11-01 00:55:52", updated_at: "2020-11-01 00:55:52">
自己紹介文は@userに入っています…しかし保存はできません…
binding.pryにて@user.errors.messagesを追加
[1] pry(#<Users::RegistrationsController>)> @user.update ArgumentError: wrong number of arguments (given 0, expected 1) from /Users/eleaco/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/persistence.rb:616:in `update' [2] pry(#<Users::RegistrationsController>)> @user.errors.messages => {:password=>["can't be blank", "is invalid"], :current_password=>["can't be blank"], :profile=>[]}#profileに記述しましたが空のようです [3] pry(#<Users::RegistrationsController>)>
user.rb
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable with_options presence: true do |i| i.validates :nickname i.validates :email i.validates :password i.validates :encrypted_password #profileを記述していない!!ここでしょうか…? end
schema.rb
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_10_23_085817) do create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false t.bigint "record_id", null: false t.bigint "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", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", 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", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id", null: false t.integer "photo_id", null: false t.text "text" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id", null: false t.integer "photo_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "photos", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.text "explanation", null: false t.string "title", null: false t.integer "user_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "relationships", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "following_id", null: false t.integer "follower_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["follower_id", "following_id"], name: "index_relationships_on_follower_id_and_following_id", unique: true end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "nickname", null: false t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.text "profile" t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: 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 add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" end
このようなコードになっております
皆様のお力をお貸しください!
よろしくお願いいたします!
回答1件
あなたの回答
tips
プレビュー