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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

5411閲覧

[devise]新規登録ができない。

NakayaKousuke

総合スコア9

Devise

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/08/20 14:32

#状況
deviseのgemをbundle installをして、新規登録をしようとするとターミナルで「Unpermitted parameters: :family_name, :first_name, :gender, :age」とエラーが出ています。

family_nameもfirst_nameもgenderもageも全てストロングパラメータで許可をしているのですが、なぜ許可されていないパラメータとなってしまうのかが分かりません。。。

どなたかご教示いただけると幸いです。

参考として、userに関するmodelとcontrollerとmigrateファイルの記述を下記に載せています。

#エラー動作動画
https://gyazo.com/1780340193890dffca76438873950756

#ターミナルのエラー

Started POST "/users" for ::1 at 2020-08-20 23:29:54 +0900 Processing by Devise::RegistrationsController#create as HTML Parameters: {"authenticity_token"=>"cYyvSjJ2E3x/VmDToVu/TtEEX3qifJZLtd/XwWr3xkBXEjwXAs9ikAdajLHo6jPQ6fDPyLEKzKamfj2m81qCdg==", "user"=>{"family_name"=>"佐藤", "first_name"=>"健", "gender"=>"男性", "age"=>"28", "email"=>"test@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"登録する"} Unpermitted parameters: :family_name, :first_name, :gender, :age (0.2ms) BEGIN User Exists? (1.8ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test@gmail.com' LIMIT 1 CACHE User Exists? (0.0ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test@gmail.com' LIMIT 1 (0.2ms) ROLLBACK Rendering devise/registrations/new.html.haml within layouts/application Rendered layouts/_header-sub.html.haml (Duration: 0.1ms | Allocations: 39) Rendered devise/shared/_links.html.haml (Duration: 0.1ms | Allocations: 87) Rendered devise/shared/_error_messages.html.haml (Duration: 0.8ms | Allocations: 719) Rendered layouts/_footer.html.haml (Duration: 0.1ms | Allocations: 10) Rendered devise/registrations/new.html.haml within layouts/application (Duration: 4.9ms | Allocations: 4545) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 247ms (Views: 22.6ms | ActiveRecord: 2.2ms | Allocations: 17878)

#app/models/user.rb

class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :family_name, presence: true validates :first_name, presence: true validates :gender, presence: {massage: "選択して下さい"} validates :age, presence: {massage: "選択して下さい"} validates :email, presence: true, uniqueness: {case_sensitive: true}, format: {with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i} enum gender: { "男性": 1, "女性": 2, "その他": 3 }, _prefix: true enum age: { "20": 1, "21": 2, "22": 3, "23": 4, "24": 5, "25": 6, "26": 7, "27": 8, "28": 9, "29": 10, "30": 11, "31": 12, "32": 13, "33": 14, "34": 15, "35": 16, "36": 17, "37": 18, "38": 19, "39": 20, "40": 21, "41": 22, "42": 23, "43": 24, "44": 25, "45": 26, "46": 27, "47": 28, "48": 29, "49": 30, "50": 31, "51": 32, "52": 33, "53": 34, "54": 35, "55": 36, "56": 37, "57": 38, "58": 39, "59": 40, "60": 41, "61": 42, "62": 43, "63": 44, "64": 45, "65": 46, "66": 47, "67": 48, "68": 49, "69": 50, "70": 51, "71": 52, "72": 53, "73": 54, "74": 55, "75": 56, "76": 57, "77": 58, "78": 59, "79": 60, "80": 61, "81": 62, "82": 63, "83": 64, "84": 65, "85": 66, "86": 67, "87": 68, "88": 69, "89": 70, "90": 71, "91": 72, "92": 73, "93": 74, "94": 75, "95": 76, "96": 77, "97": 78, "98": 79, "99": 80, "100": 81 }, _prefix: true end

#app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] before_action :configure_account_update_params, only: [:update] def new super end def create @user = User.new(user_params) unless @user.valid? flash.now[:alert] = @user.errors.full_messages render :new and return end end def edit super end def update super end def destroy super end private def user_params params.require(:user).permit(:family_name, :first_name, :gender, :age) end end

#db/migrate/20200817075114_devise_create_users.rb

class DeviseCreateUsers < ActiveRecord::Migration[6.0] def change create_table :users do |t| ## Database authenticatable t.string :family_name, null: false t.string :first_name, null: false t.integer :gender, null: false t.integer :age, null: false 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, :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

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

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

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

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

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

guest

回答1

0

ベストアンサー

公式リポジトリの説明は読みましたか?
あまり難しい使い方をしてない限りは、説明通りで動くのではないでしょうか。
https://github.com/heartcombo/devise#strong-parameters

多分、こんな感じ。

ruby

1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 protected 5 6 def configure_permitted_parameters 7 devise_parameter_sanitizer.permit(:sign_up, keys: [:family_name, :first_name, :gender, :age]) 8 end 9end

投稿2020/08/21 14:45

Cojiro

総合スコア539

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

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

NakayaKousuke

2020/08/22 07:06

回答ありがとうございます! 公式リポジトリを読みまして、無事にユーザー登録することができました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問