前提・実現したいこと
railsでdeviseをbundle installして、新規登録機能の実装をしています。
マイグレーションファイルに必要なカラムを追加して、実際にアカウントを作成しようとしましたが、全てに「can't be blank」のエラーが出てしまいます。
発生している問題・エラーメッセージ
■エラーフォーム 6 errors prohibited this user from being saved: ・First name can't be blank ・Last name can't be blank ・Sex can't be blank ・Age can't be blank ・Phone number can't be blank ・Reset password token can't be blank
■ターミナル ActiveRecord::SchemaMigration Load (5.5ms) SELECT `schema_migrations`.* FROM `schema_migrations` Processing by Devise::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"3oLEVn3jZGpvpPV8DPxugaqjj/Vnmp9NIwPeI0P25tX5HZ1CPOAGdmxMQAvp1q0Y6wuICPaNH6FbEeBgZ3mkuw==", "user"=>{"first_name"=>"nakaya", "last_name"=>"kousuke", "sex"=>"0", "age"=>"28", "phone_number"=>"09000000000", "email"=>"aaaa@aaaa", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"新規登録"} Unpermitted parameters: first_name, last_name, sex, age, phone_number (0.3ms) BEGIN User Exists (9.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaaa@aaaa' LIMIT 1 User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`phone_number` IS NULL LIMIT 1 User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaaa@aaaa' LIMIT 1 User Exists (0.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`reset_password_token` IS NULL LIMIT 1 (0.1ms) ROLLBACK Rendering devise/registrations/new.html.haml within layouts/application Rendered devise/shared/_links.html.haml (7.2ms) DEPRECATION WARNING: [Devise] `DeviseHelper.devise_error_messages!` is deprecated and it will be removed in the next major version. To customize the errors styles please run `rails g devise:views` and modify the `devise/shared/error_messages` partial. (called from block in _app_views_devise_registrations_new_html_haml___549416490695368335_70108069833140 at /Users/nakaya-kousuke/projects/smot/app/views/devise/registrations/new.html.haml:11) Rendered devise/shared/_error_messages.html.haml (3.4ms) Rendered devise/registrations/new.html.haml within layouts/application (41.8ms) Completed 200 OK in 406ms (Views: 309.3ms | ActiveRecord: 23.7ms)
該当のソースコード
■db > migrate > 20200508073156_devise_create_users.rb # frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[5.0] def change create_table :users do |t| ## Database authenticatable t.string :first_name, null: false t.string :last_name, null: false t.string :sex, null: false t.string :age, null: false t.string :phone_number, null: false, default: "" t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :users, :first_name, unique: true add_index :users, :last_name, unique: true add_index :users, :sex, unique: true add_index :users, :age, unique: true add_index :users, :phone_number, unique: true add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end
■app > models > user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :first_name, presence: true validates :last_name, presence: true validates :sex, presence: true validates :age, presence: true validates :phone_number, presence: true, uniqueness: true validates :email, presence: true, uniqueness: true validates :reset_password_token, presence: true, uniqueness: true end
■app > views > devise > registrations > new.html.haml #account-page.account-page .account-page__logo .logo smot .account-page__inner.clearfix .account-page__inner--top %h1 新規登録 = render "devise/shared/links" .account-page__inner--bottom = form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| = devise_error_messages! .account-page__inner--bottom__box .field .field-input = f.text_field :first_name, autofocus: true, placeholder: "姓", class: 'first_name' .field .field-input = f.text_field :last_name, autofocus: true, placeholder: "名", class: 'last_name' .account-page__inner--bottom__box .field .field-input = f.select :sex, [["男性", 0],["女性", 1],["その他", 2]], {}, {class: 'sex'} .field .field-input = f.text_field :age, autofocus: true, placeholder: "年齢(20歳以下は登録不可)", class: 'age' .field .field-input = f.text_field :phone_number, autofocus: true, placeholder: "電話番号(ハイフンなし)", class: 'phone_number' .field .field-input = f.email_field :email, autocomplete: "email", placeholder: "登録するメールアドレス", class: 'email' .field .field-input = f.password_field :password, autocomplete: "off", placeholder: "パスワード(英数字8文字以上)", class: 'password' .field .field-input = f.password_field :password_confirmation, autocomplete: "off", placeholder: "パスワードの確認(英数字8文字以上)", class: 'password_confirmation' .actions = f.submit "新規登録", class: 'btn'
試したこと
「rails 新規登録 エラー」や「rails devise エラー」などでググり、いろんな記事を読み漁りました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/13 09:52
2020/05/13 10:04
2020/05/13 11:47