LoginSignup
7
9

More than 5 years have passed since last update.

deviseでカラムを追加した時

Last updated at Posted at 2016-05-17

カスタマイズ用のビューファイルを生成する

$ rails g devise:views

-v 生成するビューコマンドで生成するビューファイルを指定することができる。

今回は新規登録用のビューファイルregistationsとログイン用のビューファイルsessionを指定する。

$ rails g devise:views -v registrations sessions

フォームを追加する。今回は名前を追加する。

registrations/new.html.erb
<%= form_for...do |f| %>

<%= f.text_field :name %>

<% end %>

nameの入力フォームはできたが、deviseはデフォルトではemailとpasswordの値しか保存できないようになっている。nameの値を保存したい場合は、applicationコントローラで下図のメソッドを作成し、before_actionで呼び出す必要がある。
これはストロングパラメータのようなもの。

application_controller.rb
class ApplicationController < ActionController::Base

before_action :configure_permitted_parameters, if: :devise_controller?

private
def configure_permitted_parameters
 devise_parameter_sanitizer.for(:sign_up) << :name
end
7
9
1

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
7
9