logesh
logesh

Reputation: 2662

How to add a custom field to devise in Rails 4 and customise the registration controller?

I am trying to add devise to the rails 4 app and i want to add new fields in registration and remove some existing field. I have changed my routes to

devise_for :users, :controllers => {:registrations => 'registrations'}

and i have included the following line in my application_controller.rb

class ApplicationController < ActionController::Base

  before_filter :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :password) }
  end

and i have the following in my model file (user.rb)

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable

  attr_accessor :username
end

and my registration_controller.rb contains

class RegistrationsController < Devise::RegistrationsController
        skip_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
        prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

        # GET /resource/sign_up
        def new
            super
        end

        # POST /users
        def create
            build_resource(sign_up_params)

            respond_to do |format|
                if resource.save
                    format.html { redirect_to profile_update_path, notice: 'User was successfully updated.' }
                else
                    format.html { render action: "new" }
                end
            end
        end

        protected

        def sign_up_params
            devise_parameter_sanitizer.sanitize(:sign_up)
        end

    end

the result is

Started POST "/users" for 127.0.0.1 at 2014-03-05 20:11:43 +0530
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"/Tu/QaH1gQgr73uND+fYcLzwer4yhaserghjNQxqazp=", "user"=>{"username"=>"testname", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "users" ("created_at", "encrypted_password", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Wed, 05 Mar 2014 14:41:43 UTC +00:00], ["encrypted_password", "$2a$10$9dLwOBN4qEc3Vgv8NiMVlOaOG.j4jbKNIEg1RPZPdohZYZsZQBY.."], ["updated_at", Wed, 05 Mar 2014 14:41:43 UTC +00:00]]
   (39.4ms)  COMMIT
Redirected to http://localhost:3000/profile
Completed 302 Found in 114ms (ActiveRecord: 40.0ms)

it does not save the username in the table. Please tell me where i went wrong. Thanks in advance!

UPDATE

The following is the list of fields i have in the table

id | email |                      encrypted_password                      | reset_password_token | reset_password_sent_at | remember_created_at | sign_in_count | current_sign_in_at | last_sign_in_at | current_sign_in_ip | last_sign_in_ip |         created_at         |         updated_at         | username | first_name | last_name |

Upvotes: 0

Views: 1100

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53038

If you already have username in the table. Just remove attr_accessor :username from the model as that messes up ActiveRecord. ActiveRecord thinks that username is instance variable and sets it to nil. The username value you passed is lost so it doesn't get stored in database.

Upvotes: 1

gregates
gregates

Reputation: 6714

Is there a username column in the users table in the db? I would guess not, given that you've declared attr_accessor :username -- an ActiveRecord model should automatically create accessors for its database fields. So my guess is that username isn't saved in the database because there isn't a username field in the database. Did you do a migration when you added the User model?

If there is a username field, try removing attr_accessor :username. You're probably overwriting the built-in ActiveRecord getter/setter methods.

Upvotes: 2

Related Questions