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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

2175閲覧

Rails devise(given 0, expected 1)の解決方法を教えていください!

EleAco

総合スコア15

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/03 03:46

編集2020/11/03 07:46

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

このようなコードになっております
皆様のお力をお貸しください!
よろしくお願いいたします!

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

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

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

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

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

winterboum

2020/11/03 04:00

どのfileのどの行かという情報もあるとおもうのですが、 エラーは全文のせて
winterboum

2020/11/03 04:03

account_update_params が載っていないようです
EleAco

2020/11/03 04:18

winterboum様 ご指摘ありがとうございます! エラー文を追加いたしました account_update_params が載っていないようです> こちらはどのように記述するのが正しいのでしょうか… 理解ができずすみません(;_;) 引き続きよろしくお願いいたいします
winterboum

2020/11/03 04:23

多分そういうmethodが有るはずですので、そのcodeを
asm

2020/11/03 04:28

> [2] pry(#<Users::RegistrationsController>)> @user.update あなたがpry上で「@user.update」と入力しているように見えますが その理解でよろしいでしょうか?
EleAco

2020/11/03 06:32

asmさん質問ありがとうございます 質問の内容に追加で記述します! asmさんの解釈でお間違えありません! binding.pryをregistrarions_controller.rbのupdateに記述してます
winterboum

2020/11/03 07:20 編集

Userのvalidation載せてください」
winterboum

2020/11/03 07:34 編集

あまちがい取消書き直します
guest

回答1

0

ベストアンサー

passwordが常に必要とされてます。必要なのは登録時とpassword変更時だと思います。
ので、このblockから外し on: :create オプションつけましょう。
:current_password も要求されてますこれはここを参考にしてください。
profile のエラーはなんだろう。。。
schema見せてください

投稿2020/11/03 07:42

winterboum

総合スコア23331

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

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

EleAco

2020/11/03 07:51

winterboumさん 長々と付き合っていただきありがとうございます(;_;) 新規登録時はnicknameとemailとpasswordとcurrent_passwordを記入しないといけない状態で ユーザー情報の編集時に、自己紹介文(profile)を追加しようとしています #本当はアバター(avatar)の登録もしようと考えていましたがまだ実装してません とりあえずプロフィールだけでもしたい感じです 先にschemaを追加しておきます! 参考ページありがとうございます!参考にしてオプションつけてきます!!
winterboum

2020/11/03 07:52

あ、profileのエラーは[]空か。password関係だけ潰せば多分行けます
EleAco

2020/11/03 10:26

winterboumさん 教えていただいたサイトを参考に記述を行いcurrent_passwordは消すことができました ありがとうございます!! しかし:passwordが消えません…(;_;) ``` [2] pry(#<Users::RegistrationsController>)> @user.errors.messages => {:password=>["is invalid"], :profile=>[]} ``` とでます ``` with_options presence: true do |i| i.validates :nickname i.validates :email i.validates :encrypted_password end validates :password, presence: true, on: :create ``` 記述はこの形で合っていますでしょうか…?
winterboum

2020/11/03 11:55

合ってますね。 どんなエラーになります?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問