LoginSignup
66
66

More than 5 years have passed since last update.

Rails4のdeviseでユーザ登録→確認→完了画面を作成する

Posted at

Rails認証エンジンDeviseでユーザー登録→確認→完了画面を作成する

:octocat: https://github.com/plataformatec/devise

前提

  • Deviseがインストールされていること
  • DeviseのView/Controllerカスタマイズ済み(※1)

※1
DeviseのViewカスタマイズ
Rails4でdeviseのViewをカスタマイズする

DeviseのControllerカスタマイズ
Rails4でdeviseのControllerをカスタマイズする

ルーティング

routes.rb
devise_for :users, :controllers => {
 :registrations => 'users/registrations'
}

devise_scope :user do 
 post 'users/sign_up/confirm' => 'users/registrations#confirm'
 post 'users/sign_up/complete' => 'users/registrations#complete' 
end

細かいルーティングを設定したい場合はdevise_scope内に記述する必要があるようです。

Controller

app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
 before_action :create, only: [:complete]

  def confirm 
    @user = User.new(sign_up_params)
    if @user.valid?
      render :action => 'confirm'
    else
     render :action => 'new'
    end
  end

  def complete
    render :action => 'complete'
  end
end

View

  • ユーザ登録入力画面

post先を下記に変更

app/views/devise/registrations/new.html.erb
<%= form_for @user, url: {action: "confirm"} do |f| %>
  • 確認画面
app/views/devise/registrations/confirm.html.erb
<%= form_for @user, url: {action: "complete"} do |f| %>
  <%= devise_error_messages! %>
  <div class="field">
    <%= f.label :email %>
    <%= f.hidden_field :email %>
    <%= @user.email %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <%= f.hidden_field :password %>
    <%= @user.password %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>

<% end %>

<%= render "devise/shared/links" %>

  • 完了画面
app/views/devise/registrations/complete.html.erb
  <div class="field">
    登録完了!!おつかれした。
  </div>

所感

RESTを考慮したフレームワークのためか、確認・完了画面を追加するとroutes.rbが汚くなった印象。
もう少し何かイケテル方法はないのだろうか。。。

参考

Ruby on Rails 4 でお問い合わせフォーム(確認画面つき)を作成する

66
66
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
66
66