Timofey A Bogdanov
Timofey A Bogdanov

Reputation: 25

Permiting :firstname but not saving to DB with devise registration

I'm trying to permit :firstname when registering an account, but the params are not passed through, what am I doing wrong?

I have already migrated a :firstname string into the database

# 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, :firstname])
  # 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, :firstname])
  # end

Upvotes: 1

Views: 429

Answers (3)

Violeta
Violeta

Reputation: 710

You need to do couple things:

  1. Generate the registrations controller. You can do that with this command:
rails g devise:controllers users -c=registrations
  1. In the routes.rb file, add this:
devise_for :users, controllers: { registrations: 'users/registrations' }
  1. In the registrations_controller.rb permit the extra params:

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]

  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: [:firstname])
  end
end

Reference:

https://github.com/plataformatec/devise/wiki/Tool:-Generate-and-customize-controllers

Upvotes: 2

Vibol
Vibol

Reputation: 1168

You already read this code, to do so you just need to uncomment configure_sign_up_params method and add your attributes to the keys, something like the code below.

# 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: [:firstname])
  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, :firstname])
  # end

Upvotes: 1

kishore cheruku
kishore cheruku

Reputation: 521

We may have get some problem while using sanitize for custom attributes in devise. Please follow this Add Custom Attributes .

Upvotes: 0

Related Questions