Ruby on railsで新規登録機能をdeviseにより実装中に問題が発生しました。
実現したいことは新規登録フォームでsubmitボタンを押すと登録がうまくいけばTOPページにリダイレクト、うまくいかなければ新規登録フォームにrenderすることです。
エラーメッセージが表示されて指示通りに「and return」を追記するがエラーが治らない。
AbstractController::DoubleRenderError in Users::RegistrationsController#create
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
app/controllers/Registrations_controller.rb
# frozen_string_literal: true
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
def new
super
@user = User.new
end
# POST /resource
def create
super
@user = User.new(user_params)
if @user.save
flash[:notice] = "会員登録完了あなたは#{@user.id}人目のサービス利用者なので貢献度を#{@user.id}ポイントプレゼントします(´・ω・`)"
@imalevel = 1
@imalevel += @user.id
redirect_to("/") and return
else
flash[:alert] = "会員登録失敗"
render action: :new and return
end
end
def user_params
params.require(:user).permit(:name, :user_id, :password, :level, :mailmagazin, :delete, :email, :encrypted_password, :reset_password_token)
end
# GET /resource/edit
def edit
super
end
# PUT /resource
def update
super
end
# DELETE /resource
def destroy
super
end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
#
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end
#
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
#
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
#
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end