前提・実現したいこと
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)
Loginを押した後
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
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :books, dependent: :destroy has_one_attached :profile_image def get_profile_image if profile_image.attached? profile_image else 'sample-author1.jpg' end end end
registrations/new.html.erb
Rails
<h2>Sign up</h2> <%= form_with model: @user, url: user_registration_path, local: true do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <div class="field"> <%= f.label :name %><br/> <%= f.text_field :name, autofocus: true %> </div> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <div class="actions"> <%= f.submit "Sign up" %> </div> <% end %> <%= render "devise/shared/links" %>
config/initializers/devise.rb(コードが多く、画像を投稿させていただきました。)
application_controller.rb
Rails
class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? def after_sign_in_path_for(resource) user_path(current_user.id) end private def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up,keys:[:email,:remember_me]) end end
sessions/new.html.erb
Rails
<%= form_with model: @user, url: user_registration_path, local: true do |f| %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name, autofocus: true, autocomplete: "name" %> </div> <div class="field"> <%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "current-password" %> </div> <% if devise_mapping.rememberable? %> <div class="field"> <%= f.check_box :remember_me %> <%= f.label :remember_me %> </div> <% end %> <div class="actions"> <%= f.submit "Log in" %> </div> <% end %> <%= render "devise/shared/links" %>
試したこと
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]
以上、よろしくお願いいたします。
まだ回答がついていません
会員登録して回答してみよう