Search code examples
ruby-on-railsrubydevise

Devise: custom fields not saved after signing up Rails 4


I have installed devise gem in my app for registration. I have same issue like this question

I have generated Doctor model by

rails generate devise Doctor

And here is doctor.rb

class Doctor < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

I also generated controller by rails generate devise:controllers doctors

class Doctors::RegistrationsController < Devise::RegistrationsController
before_filter :sign_up_params, only: [:create]
before_filter :account_update_params, only: [:update]
#
#  # GET /resource/sign_up
#  def new
#     super
#  end
##
##  # POST /resource
#  def create
#     super
#  end
##
##  # GET /resource/edit
#  def edit
#     super
#  end
##
##  # PUT /resource
#  def update
#     super
#  end
##
##  # DELETE /resource
#  def destroy
#     super
#  end

  protected

  def sign_up_params
    params.require(:doctor).permit(:first_name, :last_name, :email, :password, :password_confirmation, :gender, :contact_no, :about_me, :certification, :exp_summary, :username)
  end
  #
  def account_update_params
    params.require(:doctor).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password , :gender, :contact_no, :about_me, :certification, :exp_summary, :username)
  end
  # protected

  # You can put the params you want to permit in the empty array.
  #def configure_sign_up_params
  #   devise_parameter_sanitizer.for(:sign_up) << :first_name, :last_name, :gender, :contact_no, :about_me, :certification, :exp_summary, :username
  #end

  # You can put the params you want to permit in the empty array.
  #def configure_account_update_params
  #   devise_parameter_sanitizer.for(:account_update) << :first_name, :last_name, :gender, :contact_no, :about_me, :certification, :exp_summary, :username
  #end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
end

I have written in routes.rb file devise_for :doctor, :controllers => { sessions: "doctors/sessions"}.

Here is my logs from terminal after submitting Sign_up form

Started POST "/doctor" for 127.0.0.1 at 2014-12-04 16:52:20 +0530
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8Dd5u5Qq+kLyAI+RaEuoSyjsxteHw4VBndQC+W5yjy0=", "doctor"=>{"username"=>"Test5", "first_name"=>"John", "last_name"=>"Smith", "contact_no"=>"8787878787", "gender"=>"true", "email"=>"john@smith.com", "about_me"=>"Test", "certification"=>"Test", "exp_summary"=>"Test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: username, first_name, last_name, contact_no, gender, about_me, certification, exp_summary
   (0.2ms)  BEGIN
  Doctor Exists (0.3ms)  SELECT  1 AS one FROM `doctors`  WHERE `doctors`.`email` = BINARY 'john@smith.com' LIMIT 1
  SQL (0.2ms)  INSERT INTO `doctors` (`created_at`, `email`, `encrypted_password`, `updated_at`) VALUES ('2014-12-04 11:22:20', 'john@smith.com', '$2a$10$as.WAOu05ET7RUtnsdTC2ucqotK5Ls2Z6iKWI.wW3gSuIwohYfoTW', '2014-12-04 11:22:20')
   (116.8ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.3ms)  UPDATE `doctors` SET `current_sign_in_at` = '2014-12-04 11:22:20', `current_sign_in_ip` = '127.0.0.1', `last_sign_in_at` = '2014-12-04 11:22:20', `last_sign_in_ip` = '127.0.0.1', `sign_in_count` = 1, `updated_at` = '2014-12-04 11:22:20' WHERE `doctors`.`id` = 7
   (56.6ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 254ms (ActiveRecord: 174.5ms)


Started GET "/" for 127.0.0.1 at 2014-12-04 16:52:20 +0530
Processing by HomeController#index as HTML
  Doctor Load (0.3ms)  SELECT  `doctors`.* FROM `doctors`  WHERE `doctors`.`id` = 7  ORDER BY `doctors`.`id` ASC LIMIT 1
  Rendered home/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 32ms (Views: 30.5ms | ActiveRecord: 0.3ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530


Started GET "/assets/home.css?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530


Started GET "/assets/home.js?body=1" for 127.0.0.1 at 2014-12-04 16:52:20 +0530

Why my custom fields are not saved? Where I do mistake?

Update

Controller Structure:

controllers
   -> doctors
       -> confirmations_controller.rb
       -> omniauth_callbacks_controller.rb 
       -> passwords_controller.rb
       -> registrations_controller.rb
       -> sessions_controller.rb
       -> unlocks_controller.rb
   -> application_controller.rb
   -> home_controller.rb

Solution

  • EDITED:

    Okay! You were trying to override the devise controllers which is not required in your case. Follow these simple steps:

    rails g model Doctor
    

    Create your fields except email and password. Devise will take care of that.

    rails g devise:install
    
    rails g devise Doctor
    

    In your ApplicationController:

    before_action :configure_permitted_parameters, if: :devise_controller?
    
    def configure_permitted_parameters
       devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :address, :phone, :email) }
    end