teratail header banner
teratail header banner
質問するログイン新規登録

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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

923閲覧

deviseのユーザー登録失敗後に再度ユーザー登録画面にredirectしたい

adonson

総合スコア12

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

Ruby on Rails

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

0グッド

1クリップ

投稿2020/02/24 01:24

0

1

#環境設定
ruby '2.5.1'
rails '~> 5.2.4', '>= 5.2.4.1'
mysql2 '>= 0.4.4', '< 0.6.0'
devise 4.7.1

#質問内容
表題と通り、ユーザー登録が項目未記入などの理由で登録失敗した際に、
登録画面にリダイレクトしたいと思います。
登録成功時は「after_sign_up_path_for(resource)」を使ってトップページの飛ばしているのですが、
失敗時はどのようにすれば良いでしょうか。

#app/controllers/users/registorations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController # before_action :configure_sign_up_params, only: [:create] # before_action :configure_account_update_params, only: [:update] def prefecture @prefectures = Prefecture.all end # def create # if @user.empty? # # Handle a successful save. # else # redirect_to signup_path # end # end # GET /resource/sign_up # def new # super # end # POST /resource # def create # super # 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 def detail @user = User.find_by(id: params[:id]) 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) "/" end # The path used after sign up for inactive accounts. # def after_inactive_sign_up_path_for(resource) # redirect_to signup_path # end end

#config/routes.rb

Rails.application.routes.draw do resources :prefectures root to: 'top#index' resources :items, only: [:new] devise_for :users, :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions' } resources :orders, only: [:new] devise_scope :user do get "user/:id", :to => "users/registrations#detail" get "signup", :to => "users/registrations#new" get "login", :to => "users/sessions#new" get "logout", :to => "users/sessions#destroy" end end

app/views/devise/registorations/new.html.haml

.registrationsNew %h2< 会員登録 = form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| -#= render "devise/shared/error_messages", resource: resource .request 全て必須事項です。 %span.nickname = f.label :nickname = f.text_field :nickname %br/ %span.email = f.label :email = f.email_field :email, autofocus: true, autocomplete: "email" %br/ %span.password = f.label :password - if @minimum_password_length %em (#{@minimum_password_length} 文字以上の英数字) = f.password_field :password, autocomplete: "new-password" %br/ %span.passwordConfirm = f.label :password_confirmation = f.password_field :password_confirmation, autocomplete: "new-password" %br/ %span.lastName = f.label :last_name = f.text_field :last_name, autofocus: true, autocomplete: "last_name" %span.firstName = f.label :first_name = f.text_field :first_name, autofocus: true, autocomplete: "first_name" %br/ %span.lastNameKana = f.label :last_name_kana = f.text_field :last_name_kana, autofocus: true, autocomplete: "last_name_kana" %span.firstNameKana = f.label :first_name_kana = f.text_field :first_name_kana, autofocus: true, autocomplete: "first_name_kana" %br/ %span.tel = f.label :tel = f.telephone_field :tel, autofocus: true, autocomplete: "tel" %br/ %span.zipCode = f.label :zip_code = f.text_field :zip_code, autofocus: true, autocomplete: "zip_code" %br/ %span.prefecture = f.label :prefecture = f.collection_select :prefecture_id, Prefecture.group(:prefecture).order(id: :asc), :id, :prefecture, {prompt: ""} %br/ %span.address = f.label :address = f.text_field :address, autofocus: true, autocomplete: "address" %br/ %span.birth 生年月日(西暦) %span.birthYear = f.label :birth_year = f.number_field :birth_year, autofocus: true, autocomplete: "birth_year", min:1900, max:2020,value:2000 %span.birthMonth = f.label :birth_month = f.number_field :birth_month, autofocus: true, autocomplete: "birth_month", min:1, max:12,value:1 %span.birthDay = f.label :birth_day = f.number_field :birth_day, autofocus: true, autocomplete: "birth_day", min:1, max:31,value:1 .actions = f.submit "登録" = render "devise/shared/links"

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

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

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

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

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

guest

回答1

0

自己解決

解決できました。
下記のその方法を残しておきます。
https://qiita.com/adonson/items/ab81bcbe2ef9c37d60c3

投稿2020/02/25 03:24

adonson

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.30%

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

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

質問する

関連した質問