0

I want to add a property name in the user model. I ran the migration command to add the column to the database and that worked. Adding the property to user itself worked as well but it isn't saved in the db.

How can I add the property "name" to the required params of the sign_up and account_update of RegistrationController?

This is my user model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessor :name
end

I tried adding the required params to the methodes like this in the RegistrationController

class Users::RegistrationsController < Devise::RegistrationsController

  def sign_up_params
    params.require(:user).permit(:name,:email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation, :current_password)
  end
end

In the routings i added the line

devise_for :users, controllers: { registrations: 'users/registrations' }

But the name of the user still isn't saved in the database.

Kupi
  • 903
  • 1
  • 10
  • 16

2 Answers2

3

Add this to your ApplicationController to configure signup signin & account_update params.

def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name,:email, :password, :password_confirmation) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :username, :anonymous, :email, :password, :password_confirmation,:reset_password_token) }
end

And also add before_filter to ApplicationController like :

before_filter :configure_permitted_parameters, if: :devise_controller?
Muhammad Ali
  • 2,173
  • 15
  • 20
  • Seemed to work only after i removed the property i had added in my user model.. But it did work so thx! – Kupi Mar 30 '16 at 12:05
0

Please check Devise Parameter Sanitization

You can try this:

class Users::RegistrationsController < Devise::RegistrationsController

 def sign_up_params
  devise_parameter_sanitizer.for(:sign_up).push(:name)
 end

 def account_update_params
   devise_parameter_sanitizer.for(:account_update).push(:name, :email, :password, :password_confirmation, :current_password)
 end
end
dp7
  • 6,651
  • 1
  • 18
  • 37