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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

3646閲覧

Rails上で名前を使ってのログインができません(devise - Unpermitted parameter: :name)

iyteratail

総合スコア1

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/07/31 21:58

編集2021/07/31 22:00

前提・実現したいこと

Railsにて読んだ本の感想を投稿するアプリケーションを作成しています。
現在、deviseにてログイン部分を実装しています。

新規登録時は
0. 名前
0. メールアドレス
0. パスワード

の3つで登録し、ログインの際は
0. 名前
0. パスワード

の2つでログインできるように実装したいです。

発生している問題・エラーメッセージ

名前とパスワードの2つでログインできるように設定しましたが、ログインが正常にできない状況です。

下記のようにUnpermitted parameter: :nameと表示が出ています。

Processing by Devise::SessionsController#new as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"p2RH5biSuGTNOIsojnHm+lcJ1tMf1SKdZPbqeVmiaAgp6NvUqb+uPp8EmupVfJjxy4R+0ZVEvVTZWQgeZ/Ky5w==", "user"=>{"name"=>"test19", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} Unpermitted parameter: :name Rendering devise/sessions/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (0.8ms) Rendered devise/sessions/new.html.erb within layouts/application (4.1ms) Completed 200 OK in 387ms (Views: 82.9ms | ActiveRecord: 0.0ms)

web上でログインができないのですが、エラーメッセージ等は表示されておりません。

該当のソースコード

devise.rb

deviserb

1config.authentication_keys = [:name] 2 3 # Configure parameters from the request object used for authentication. Each entry 4 # given should be a request method and it will automatically be passed to the 5 # find_for_authentication method and considered in your model lookup. For instance, 6 # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. 7 # The same considerations mentioned for authentication_keys also apply to request_keys. 8 # config.request_keys = [] 9 10 # Configure which authentication keys should be case-insensitive. 11 # These keys will be downcased upon creating or modifying a user and when used 12 # to authenticate or find a user. Default is :email. 13 config.case_insensitive_keys = [:email] 14 15 # Configure which authentication keys should have whitespace stripped. 16 # These keys will have whitespace before and after removed upon creating or 17 # modifying a user and when used to authenticate or find a user. Default is :email. 18 config.strip_whitespace_keys = [:email]

registrations/new.html.erb

registrationsnewhtmlerb

1 2<h2>Sign up</h2> 3 4<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 5 <%= render "devise/shared/error_messages", resource: resource %> 6 7 <div class="field"> 8 <%= f.label :name %><br /> 9 <%= f.text_field :name, autofocus: true, autocomplete: "name" %> 10 </div> 11 12 <div class="field"> 13 <%= f.label :email %><br /> 14 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 15 </div> 16 17 <div class="field"> 18 <%= f.label :password %> 19 <% if @minimum_password_length %> 20 <em>(<%= @minimum_password_length %> characters minimum)</em> 21 <% end %><br /> 22 <%= f.password_field :password, autocomplete: "new-password" %> 23 </div> 24 25 <div class="field"> 26 <%= f.label :password_confirmation %><br /> 27 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 28 </div> 29 30 <div class="actions"> 31 <%= f.submit "Sign up" %> 32 </div> 33<% end %> 34 35<%= render "devise/shared/links" %> 36

session/new.html.erb

sessionnewhtmlerb

1<h2>Log in</h2> 2 3<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 4 5 <div class="field"> 6 <%= f.label :name %><br /> 7 <%= f.text_field :name, autofocus: true, autocomplete: "name" %> 8 </div> 9 10 11 <div class="field"> 12 <%= f.label :password %><br /> 13 <%= f.password_field :password, autocomplete: "current-password" %> 14 </div> 15 16 <% if devise_mapping.rememberable? %> 17 <div class="field"> 18 <%= f.check_box :remember_me %> 19 <%= f.label :remember_me %> 20 </div> 21 <% end %> 22 23 <div class="actions"> 24 <%= f.submit "Log in" %> 25 </div> 26<% end %> 27 28<%= render "devise/shared/links" %> 29

application_controller.rb

applicationcontroller

1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 def after_sign_in_path_for(resource) 5 users_path 6 #@user = User.find(params[:id]) 7 #redirect_to user_path(@user.id) 8 end 9 10 protected 11 12 def configure_permitted_parameters 13 devise_parameter_sanitizer.permit(:sign_up, keys: [:email]) 14 end 15 16end 17

devise_create_users.rb

devisecreateusersrb

1# frozen_string_literal: true 2 3class DeviseCreateUsers < ActiveRecord::Migration[5.2] 4 def change 5 create_table :users do |t| 6 ## Database authenticatable 7 t.string :email, null: false, default: "" 8 t.string :encrypted_password, null: false, default: "" 9 10 ## Recoverable 11 t.string :reset_password_token 12 t.datetime :reset_password_sent_at 13 14 ## Rememberable 15 t.datetime :remember_created_at 16 17 ## Trackable 18 # t.integer :sign_in_count, default: 0, null: false 19 # t.datetime :current_sign_in_at 20 # t.datetime :last_sign_in_at 21 # t.string :current_sign_in_ip 22 # t.string :last_sign_in_ip 23 24 ## Confirmable 25 # t.string :confirmation_token 26 # t.datetime :confirmed_at 27 # t.datetime :confirmation_sent_at 28 # t.string :unconfirmed_email # Only if using reconfirmable 29 30 ## Lockable 31 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 32 # t.string :unlock_token # Only if unlock strategy is :email or :both 33 # t.datetime :locked_at 34 35 t.string :name 36 37 t.timestamps null: false 38 end 39 40 add_index :users, :email, unique: true 41 add_index :users, :reset_password_token, unique: true 42 # add_index :users, :confirmation_token, unique: true 43 # add_index :users, :unlock_token, unique: true 44 end 45end 46

sessions_controller.rb

sessionscontroller.rb

1# frozen_string_literal: true 2 3class Users::SessionsController < Devise::SessionsController 4 # before_action :configure_sign_in_params, only: [:create] 5 6 # GET /resource/sign_in 7 # def new 8 # super 9 # end 10 11 # POST /resource/sign_in 12 # def create 13 # super 14 # end 15 16 # DELETE /resource/sign_out 17 # def destroy 18 # super 19 # end 20 21 # protected 22 23 # If you have extra params to permit, append them to the sanitizer. 24 # def configure_sign_in_params 25 # devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) 26 # end 27end 28

試したこと

下記記事を参考に実装しました
【Rails】この記事を「devise 名前 ログイン」で調べたあなたへ贈る
https://qiita.com/yuki82511988/items/73659af9d1049bd1b256

Unpermitted parameter: :nameについて検索後、sessions_controller.rbを確認

どうかよろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

applicationコントローラに下記を記載すると新規登録後に直接新規ユーザーページへ遷移することに成功。

applicationcontroller

1def after_sign_in_path_for(resource) 2 user_path(@user.id) 3end

上記が関係しているかわかりませんが、今回質問させていただいた「名前を使ってのログイン」の部分も同時に解決しました。

投稿2021/08/01 05:15

iyteratail

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問