前提・実現したいこと
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)」というエラーが発生してしまいます。
失礼な点があるかもしれませんがどうぞ宜しくおねがい致します。
回答1件
あなたの回答
tips
プレビュー