Adding extra registration fields with Devise

前端 未结 8 620
情话喂你
情话喂你 2020-12-01 06:57

I am trying to add some extra fields to registrations#new. Since I only want extra data and do not need different functionality, I don\'t see why I need to override controll

8条回答
  •  粉色の甜心
    2020-12-01 07:11

    As of Devise version 4.3.0, May 15th 2017, the solution is as follows from the documentation. In this case, the username field is being added.

    In case you want to permit additional parameters (the lazy way™), you can do so using a simple before filter in your ApplicationController:

    class ApplicationController < ActionController::Base
      before_action :configure_permitted_parameters, if: :devise_controller?
    
      protected
    
      def configure_permitted_parameters
        devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
      end
    end
    

    And of course, simply add the field to your database

    > rails g migration AddUsernameToUsers
    
    class AddUsernameToUsers < ActiveRecord::Migration[5.0]
      def change
        add_column :users, :username, :string, null: false, index: true, unique: true
      end
    end
    

    And then add the necessary fields into the view for registrations#new

    <%= f.text_field :username, placeholder: "Username"  %>
    

提交回复
热议问题