前提・実現したいこと
Ruby on Railsのdeviseを用いて名前でログイン出来る機能を
https://qiita.com/yuki82511988/items/73659af9d1049bd1b256というサイトを
参考に実装しようとしたのですが
Email can't be blankというエラーが出てログインできません。
発生している問題・エラーメッセージ
1 error prohibited this user from being saved:
Email can't be blank
ログインしようとした時
!明](83f1e0ca7a3e193f5eb38921087216c9.png)
sign upした時のターミナル
Started POST "/users" for 157.110.132.69 at 2021-12-10 00:36:03 +0000 Cannot render console from 157.110.132.69! Allowed networks: 127.0.0.0/127.255.255.255, ::1 (0.4ms) SELECT sqlite_version(*) (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC Processing by Devise::RegistrationsController#create as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"name"=>"松尾芭蕉", "email"=>"2@2130", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} TRANSACTION (0.1ms) begin transaction User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "2@2130"], ["LIMIT", 1]] User Create (0.3ms) INSERT INTO "users" ("email", "encrypted_password", "name", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["email", "2@2130"], ["encrypted_password", "$2a$12$YzNfdnH1kwkhu38MSbyf5.4d4ovUoH68ONfQnud1xVV63fIIkzAga"], ["name", "松尾芭蕉"], ["created_at", "2021-12-10 00:36:03.811852"], ["updated_at", "2021-12-10 00:36:03.811852"]] TRANSACTION (3.7ms) commit transaction Redirected to https://0d54000a22584ba9b57703c7647c58ad.vfs.cloud9.us-east-1.amazonaws.com/users/4 Completed 302 Found in 293ms (ActiveRecord: 4.6ms | Allocations: 20407) Started GET "/users/4" for 157.110.132.69 at 2021-12-10 00:36:04 +0000 Cannot render console from 157.110.132.69! Allowed networks: 127.0.0.0/127.255.255.255, ::1 Processing by UsersController#show as HTML Parameters: {"id"=>"4"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]] ↳ app/controllers/users_controller.rb:5:in `show' Rendering layout layouts/application.html.erb Rendering users/show.html.erb within layouts/application Rendered users/show.html.erb within layouts/application (Duration: 4.5ms | Allocations: 964) [Webpacker] Everything's up-to-date. Nothing to do User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ↳ app/views/layouts/application.html.erb:15 Rendered layout layouts/application.html.erb (Duration: 55.5ms | Allocations: 10897) Completed 200 OK in 82ms (Views: 57.5ms | ActiveRecord: 0.8ms | Allocations: 18101)
sign in時のターミナル
Started POST "/users" for 157.110.132.69 at 2021-12-10 00:37:38 +0000 Cannot render console from 157.110.132.69! Allowed networks: 127.0.0.0/127.255.255.255, ::1 (0.4ms) SELECT sqlite_version(*) (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC Processing by Devise::RegistrationsController#create as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"name"=>"松尾芭蕉", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} Unpermitted parameter: :remember_me Rendering layout layouts/application.html.erb Rendering devise/registrations/new.html.erb within layouts/application Rendered devise/shared/_error_messages.html.erb (Duration: 2.6ms | Allocations: 2769) Rendered devise/shared/_links.html.erb (Duration: 1.1ms | Allocations: 822) Rendered devise/registrations/new.html.erb within layouts/application (Duration: 8.9ms | Allocations: 6714) [Webpacker] Everything's up-to-date. Nothing to do Rendered layout layouts/application.html.erb (Duration: 25.8ms | Allocations: 13836) Completed 200 OK in 304ms (Views: 27.6ms | ActiveRecord: 0.6ms | Allocations: 26994)
sign in 時のターミナル
(Unpermitted parameter: :remember_meとあったのでapplication_controller.rbのconfigure_permitted_parametersにremember_meを追加後)
Started POST "/users" for 157.110.132.69 at 2021-12-10 00:39:41 +0000 Cannot render console from 157.110.132.69! Allowed networks: 127.0.0.0/127.255.255.255, ::1 (0.4ms) SELECT sqlite_version(*) (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC Processing by Devise::RegistrationsController#create as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"name"=>"松尾芭蕉", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} Rendering layout layouts/application.html.erb Rendering devise/registrations/new.html.erb within layouts/application Rendered devise/shared/_error_messages.html.erb (Duration: 7.2ms | Allocations: 2769) Rendered devise/shared/_links.html.erb (Duration: 2.1ms | Allocations: 822) Rendered devise/registrations/new.html.erb within layouts/application (Duration: 20.5ms | Allocations: 6713) [Webpacker] Everything's up-to-date. Nothing to do Rendered layout layouts/application.html.erb (Duration: 51.8ms | Allocations: 13835) Completed 200 OK in 332ms (Views: 56.6ms | ActiveRecord: 0.4ms | Allocations: 26968)
該当のソースコード
user.rb
Rails
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :books, dependent: :destroy 8 has_one_attached :profile_image 9 10 11 def get_profile_image 12 if profile_image.attached? 13 profile_image 14 else 15 'sample-author1.jpg' 16 end 17 end 18end 19
registrations/new.html.erb
Rails
1<h2>Sign up</h2> 2 3<%= form_with model: @user, url: user_registration_path, local: true do |f| %> 4 <%= render "devise/shared/error_messages", resource: resource %> 5 6 <div class="field"> 7 <%= f.label :name %><br/> 8 <%= f.text_field :name, autofocus: true %> 9 </div> 10 11 <div class="field"> 12 <%= f.label :email %><br /> 13 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 14 </div> 15 16 <div class="field"> 17 <%= f.label :password %> 18 <% if @minimum_password_length %> 19 <em>(<%= @minimum_password_length %> characters minimum)</em> 20 <% end %><br /> 21 <%= f.password_field :password, autocomplete: "new-password" %> 22 </div> 23 24 <div class="field"> 25 <%= f.label :password_confirmation %><br /> 26 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 27 </div> 28 29 <div class="actions"> 30 <%= f.submit "Sign up" %> 31 </div> 32<% end %> 33 34<%= render "devise/shared/links" %> 35
config/initializers/devise.rb(コードが多く、画像を投稿させていただきました。)
application_controller.rb
Rails
1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 def after_sign_in_path_for(resource) 5 user_path(current_user.id) 6 end 7 8 private 9 10 def configure_permitted_parameters 11 devise_parameter_sanitizer.permit(:sign_up,keys:[:email,:remember_me]) 12 end 13end 14
sessions/new.html.erb
Rails
1<%= form_with model: @user, url: user_registration_path, local: true do |f| %> 2 <div class="field"> 3 <%= f.label :name %><br /> 4 <%= f.text_field :name, autofocus: true, autocomplete: "name" %> 5 </div> 6 7 <div class="field"> 8 <%= f.label :password %><br /> 9 <%= f.password_field :password, autocomplete: "current-password" %> 10 </div> 11 12 <% if devise_mapping.rememberable? %> 13 <div class="field"> 14 <%= f.check_box :remember_me %> 15 <%= f.label :remember_me %> 16 </div> 17 <% end %> 18 19 <div class="actions"> 20 <%= f.submit "Log in" %> 21 </div> 22<% end %> 23 24<%= render "devise/shared/links" %> 25
試したこと
1 ターミナルを確認した所Unpermitted parameter: :remember_meとあったのでapplication_controller.rbのconfigure_permitted_parametersにremember_meを追加
2 sign up時にemailが登録されてないと思い、ターミナルを確認したが問題解決には至らなかった
3 emailというカラムが存在してないと思い、schema.rbを確認した
補足情報(FW/ツールのバージョンなど)
windows10
Rails 6.1.4.1 (2019-04-16 revision 67580) [x86_64-linux]
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
以上、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/10 14:20
2021/12/10 14:35