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

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

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

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

Ruby

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

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

Ruby on Rails

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

Q&A

解決済

1回答

5433閲覧

【rails】undefined method `password_digest=' for #<User id: nil, email: nil, created_at: nil, updated_

退会済みユーザー

退会済みユーザー

総合スコア0

Devise

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

Ruby

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

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/05/31 07:55

編集2020/06/01 03:58

前提・実現したいこと

Ruby on railsで「devise」を使って新規登録機能を実装したいです。

発生している問題

新規登録ボタンを押すとエラーが発生する。

エラーメッセージ

NoMethodError in Users::RegistrationsController#create undefined method `password_digest=' for #<User id: nil, email: nil, created_at: nil, updated_at: nil> Did you mean? password=

該当のソースコード

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController def new @user = User.new end def create @user = User.new(email: params[:email], password: params[:password]) if @user.save flash[:notice] = "会員登録完了あなたは#{@user.id}人目のサービス利用者です(´・ω・`)" redirect_to("/") else flash[:alert] = "会員登録失敗" render action: :new end end end

app/views/devise/new.html.erb

<h2>新規登録</h2> <% @user = User.new unless @user %> <%= form_for :@user,:url => {controller: "registrations", action: "create" } do |f| %> <%= f.email_field :email %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %>字以上で記入)</em> <% end %> <%= f.password_field :password %> <%= f.password_field :password_confirmation %> <%= f.submit "新規登録!" %> <% end %>

db/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_05_30_100655) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "posts", force: :cascade do |t| t.string "url" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false 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", 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 create_table "videos", force: :cascade do |t| t.string "video_id" t.string "title" t.string "setumei" t.string "komento" t.string "kategori" t.float "star" t.string "word" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end end

補足情報

Windows 10 home 64ビット
ruby 2.6.6
Ruby on rails 6.0.3.1
PostgreSQL 13beta1
テキストエディタ Atom

追記

app/models/user.rb

class User < ApplicationRecord has_secure_password devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :email, uniqueness: true end

試したこと1

user.rbの中の「has_secure_password」を削除したところ「ActiveModel::UnknownAttributeError in Users::RegistrationsController#create unknown attribute 'password' for User.」というエラーが発生したため、元に戻しました。

試したこと2

user.rbの「devise :rememberable」を

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

に変更しました。
すると今度は「ArgumentError in Users::RegistrationsController#create wrong number of arguments (given 0, expected 1)」というエラーが発生してしまいます。

失礼な点があるかもしれませんがどうぞ宜しくおねがい致します。

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

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

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

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

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

winterboum

2020/05/31 08:12

UserのDBに問題がありそうです。db/schema.rb載せてください。 あと、 普通 newは新規登録の入力画面を表示される actionで、 入力後処理するのは create です。 普通と違う設定にすると他人が読むとき混乱しますし、Railsに対しても余分な宣言が必要になるのでやめたほうが良いです。
退会済みユーザー

退会済みユーザー

2020/05/31 08:54

エラーを治すためにいろいろいじって変なコードになっていました。 db/schema.rbと合わせてnew、createのほうも変更しておきました。 どうぞよろしくお願い致します。
winterboum

2020/05/31 09:14

deviseの導入はどういう手順でやりました?
退会済みユーザー

退会済みユーザー

2020/05/31 09:22

覚えている範囲ですが 1. Gemfileに 'devise'を追記。 2. bundle install 3.rails g devise:install 5. rails g devise:views 6.rails g devise user 7.rails g devise:controllers users の順番です。
winterboum

2020/05/31 11:52

う〜〜むわからん。 deviseだとencrypted_passwordだったと思います。 db/schema.rbではそうなってます。 なのに password_digest= というエラー。 model User も載せていただけますか?
退会済みユーザー

退会済みユーザー

2020/05/31 23:36

追記の部分に追記しておきました!
guest

回答1

0

ベストアンサー

has_secure_passwordを消してみるとよいでしょう。


追記

database_authenticatableモジュールが無いのが問題になっていますね。

ruby

1 devise :database_authenticatable, :rememberable

に変えるとうまく行くかと思います。

なお、本来はデフォルトで以下を生成するはずです。

# Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

投稿2020/06/01 00:18

編集2020/06/01 03:33
asm

総合スコア15147

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

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

退会済みユーザー

退会済みユーザー

2020/06/01 00:43

user.rbの中の「has_secure_password」を削除したところ「ActiveModel::UnknownAttributeError in Users::RegistrationsController#createunknown attribute 'password' for User.」というエラーが発生しました。
退会済みユーザー

退会済みユーザー

2020/06/01 03:59 編集

エラーが出るのでデフォルトの値から変更していました。 「devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable」のように修正すると今度は「ArgumentError in Users::RegistrationsController#create wrong number of arguments (given 0, expected 1)」というエラーが発生してしまいます…
asm

2020/06/01 04:16

has_secure_passwordの削除を行った上でdatabase_authenticatableを追加しても駄目でしょうか?
退会済みユーザー

退会済みユーザー

2020/06/01 04:26

「has_secure_password」を削除したところエラーは消えました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問