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

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

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

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

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

1回答

1707閲覧

【rails】【devise】新規登録画面でアカウント作成をする、入力したデータが空白になっているとエラーの内容が表示される。

NakayaKousuke

総合スコア9

Devise

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

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

0クリップ

投稿2020/05/13 09:24

前提・実現したいこと

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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

全部ではないですね、passwordには出ていない。
Unpermitted parameters: first_name, last_name, sex, age, phone_number
とあるように、これらのデータは許可されていないとして除外されています。
Deviseが元から用意しているcolumn以外を ユーザー作成時 から設定するには

class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) end end

このような定義が必要です
:name のところにずらっと並べてください

投稿2020/05/13 09:36

winterboum

総合スコア23347

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

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

NakayaKousuke

2020/05/13 09:52

回答ありがとうございます! さっそくずらっと並べてみました! ``` class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :authenticate_user! before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name], [:last_name], [:sex], [:age], [:phone_number]) end end ``` これでアカウント登録すると、「SyntaxError in Devise::RegistrationsController#create」が出てしまいました。。。 構文のミスが探しきれず、でも必死に探しています。。。 エラー文のページのGyazoのURLです。 https://gyazo.com/086172f616f6928de94ff7d78ede4f28
winterboum

2020/05/13 10:04

devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name,:sex, :age, :phone_number) sex より gender のほうがしっくり来ます
NakayaKousuke

2020/05/13 11:47

[ ]の中で並べるということですね!! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問