【Rails】deviseでemail, password以外のサインアップパラメータを追加する
やりたいこと
サインアップするためのパラメータに、email, password以外に screen_name
というパラメータを追加したい。
前提
以下の手順を完了していること。 (DBのカラムは以下手順で追加済みです)
手順
$ rails g devise:views users Running via Spring preloader in process 68444 invoke Devise::Generators::SharedViewsGenerator create app/views/users/shared create app/views/users/shared/_error_messages.html.erb create app/views/users/shared/_links.html.erb invoke simple_form_for create app/views/users/confirmations create app/views/users/confirmations/new.html.erb create app/views/users/passwords create app/views/users/passwords/edit.html.erb create app/views/users/passwords/new.html.erb create app/views/users/registrations create app/views/users/registrations/edit.html.erb create app/views/users/registrations/new.html.erb create app/views/users/sessions create app/views/users/sessions/new.html.erb create app/views/users/unlocks create app/views/users/unlocks/new.html.erb invoke erb create app/views/users/mailer create app/views/users/mailer/confirmation_instructions.html.erb create app/views/users/mailer/email_changed.html.erb create app/views/users/mailer/password_change.html.erb create app/views/users/mailer/reset_password_instructions.html.erb create app/views/users/mailer/unlock_instructions.html.erb
group :development do gem 'html2haml' end
$ bundle install
$ find . -name \*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 html2haml
$ find app/views -name \*.erb app/views/users/unlocks/new.html.erb app/views/users/passwords/edit.html.erb app/views/users/passwords/new.html.erb app/views/users/mailer/confirmation_instructions.html.erb app/views/users/mailer/reset_password_instructions.html.erb app/views/users/mailer/password_change.html.erb app/views/users/mailer/email_changed.html.erb app/views/users/mailer/unlock_instructions.html.erb app/views/users/shared/_error_messages.html.erb app/views/users/shared/_links.html.erb app/views/users/sessions/new.html.erb app/views/users/confirmations/new.html.erb app/views/users/registrations/edit.html.erb app/views/users/registrations/new.html.erb
$ find app/views -name \*.haml app/views/layouts/application.html.haml app/views/layouts/mailer.text.haml app/views/layouts/mailer.html.haml app/views/users/unlocks/new.html.haml app/views/users/passwords/edit.html.haml app/views/users/passwords/new.html.haml app/views/users/mailer/unlock_instructions.html.haml app/views/users/mailer/reset_password_instructions.html.haml app/views/users/mailer/password_change.html.haml app/views/users/mailer/confirmation_instructions.html.haml app/views/users/mailer/email_changed.html.haml app/views/users/shared/_links.html.haml app/views/users/shared/_error_messages.html.haml app/views/users/sessions/new.html.haml app/views/users/confirmations/new.html.haml app/views/users/registrations/edit.html.haml app/views/users/registrations/new.html.haml
$ find app/views -name \*.erb | xargs rm
$ find app/views -name \*.erb
app/views/users/registrations/new.html.haml
%h2 ユーザー登録 = simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| = f.input :screen_name, autofocus: true = f.input :email, autocomplete: 'email' = f.input :password, autocomplete: 'new-password' = f.input :password_confirmation, autocomplete: 'new-password' = f.button :submit = render "users/shared/links"
$ rails g devise:controllers users Running via Spring preloader in process 91047 create app/controllers/users/confirmations_controller.rb create app/controllers/users/passwords_controller.rb create app/controllers/users/registrations_controller.rb create app/controllers/users/sessions_controller.rb create app/controllers/users/unlocks_controller.rb create app/controllers/users/omniauth_callbacks_controller.rb =============================================================================== Some setup you must do manually if you haven't yet: Ensure you have overridden routes for generated controllers in your routes.rb. For example: Rails.application.routes.draw do devise_for :users, controllers: { sessions: 'users/sessions' } end ===============================================================================
config/routes.rb
の devise_for :users
の部分を以下のように修正します。
devise_for :users, controllers: { registrations: 'users/registrations' }
app/controllers/application_controller.rb
を以下のようにします。keys: [:screen_name]
のところに今回追加したい screen_name
を入れます。
# frozen_string_literal: true class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:screen_name]) end end