【Rails7】Deviseでユーザーの新規登録時にrollback transactionとなり、登録できない。
解決したいこと
RailsのDeviseにてユーザーの新規登録の情報を入力し、新規登録(SignUp)のボタンを押しても、画面が変わりません。その際に、ターミナルではrollback transactionが表示されており、ユーザーの新規登録ができていないと思われます。
そのため以上のエラーを回避し、新規登録ができるようにしたいです。
よろしくお願いいたします。
rollback transactionが発生している様子
Started POST "/users" for ::1 at 2022-10-01 06:57:10 +0900
Processing by Users::RegistrationsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
TRANSACTION (0.1ms) begin transaction
User Exists? (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test@test.com"], ["LIMIT", 1]]
TRANSACTION (0.1ms) rollback transaction
Rendering layout layouts/application.html.erb
Rendering devise/registrations/new.html.erb within layouts/application
Rendered devise/shared/_error_messages.html.erb (Duration: 1.5ms | Allocations: 761)
Rendered devise/shared/_links.html.erb (Duration: 0.3ms | Allocations: 78)
Rendered devise/registrations/new.html.erb within layouts/application (Duration: 7.6ms | Allocations: 2920)
Rendered layout layouts/application.html.erb (Duration: 20.8ms | Allocations: 5363)
Completed 200 OK in 323ms (Views: 21.9ms | ActiveRecord: 0.5ms | Allocations: 10358)
試したこと
・rails consoleから直接ユーザーを作成し、user.valid?でfalseが出たため、p user.errorsで詳細を出力した。
・ストロングパラメータを追加
・以下の記事を参照
https://qiita.com/pinoko72447/questions/44effbd8cb15f2127287
https://qiita.com/magatama/items/c5705e45ce8d60f925ff
rails consoleからユーザーを作成した様子
PS C:\Users\> rails c
Loading development environment (Rails 7.0.4)
irb(main):001:0> user=User.new(email:"test@test.com",password:"testtest")
=> #<User id: nil, email: "test@test.com", created_at: nil, updated_at: nil>
irb(main):002:0> user.valid?
User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test@test.com"], ["LIMIT", 1]]
=> false
irb(main):003:0> p user.errors
#<ActiveModel::Errors [#<ActiveModel::Error attribute=email, type=taken, options={:allow_blank=>true, :if=>:will_save_change_to_email?, :value=>"test@test.com"}>]>
=> #<ActiveModel::Errors [#<ActiveModel::Error attribute=email, type=taken, options={:allow_blank=>true, :if=>:will_save_change_to_email?, :value=>"test@test.com"}>]>
ストロングパラメータ
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [ :email, :encrypted_password])
end
end
new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<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" %>
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
end
schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_09_27_224032) do
create_table "posts", force: :cascade do |t|
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
0