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

logesh

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 |
gregates

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ruby on Rails: Custom Devise Registration Controller, Asking For Create Action

From Dev

Devise custom validation for a custom field in the registration form

From Dev

Rails 4 updating field in devise registrations controller before saving user

From Dev

How to Debug Custom Devise Strategy in Rails 4?

From Dev

How to Debug Custom Devise Strategy in Rails 4?

From Dev

Updating custom field for devise user model in Rails 4

From Dev

Rails - Devise - Add action to controller

From Dev

How to generate Registration controller of devise gem

From Dev

Rails 4: how to access the content of Devise Registrations Controller

From Dev

Rails 4 Devise custom routes

From Dev

Rails 4 Devise custom routes

From Dev

Having trouble adding a "custom" field to a Devise registration form

From Dev

Rails, use a form field to set something on the user registration (devise)

From Dev

Devise sign up - how to save an attribute without having a form field for it (Rails4, devise)

From Dev

Ruby on Rails: How to customise error message for MailForm when field is blank

From Dev

Add a Time column to Devise and have it updated manually from controller in Rails 4

From Dev

Rails 4 - devise_for not using custom controllers

From Dev

Rails 4 + Custom Devise attributes -- ParameterSanitizer Error

From Dev

Add Custom Type to Rails User Registration

From Dev

Unable to add custom fields to my Devise user model in Rails 4 with strong parameters

From Dev

Unable to add custom fields to my Devise user model in Rails 4 with strong parameters

From Dev

Yii - How to add custom field to form that can be accesed in controller?

From Dev

How to add a custom route, controller, and action in Ruby on Rails?

From Dev

How to setup angular-rails 4.2 - devise registration/authentication?

From Dev

How do I validate certain fields with rails devise on registration only

From Dev

In Rails 4, how can I get data out of a devise user within a controller?

From Dev

How do I use objects in controller for viewing data in Rails 4 devise sign_in page

From Dev

Devise rails 4 - Can't edit my new.html.erb file under devise registration

From Dev

Proper way of rails routing custom Users controller and devise gem

Related Related

  1. 1

    Ruby on Rails: Custom Devise Registration Controller, Asking For Create Action

  2. 2

    Devise custom validation for a custom field in the registration form

  3. 3

    Rails 4 updating field in devise registrations controller before saving user

  4. 4

    How to Debug Custom Devise Strategy in Rails 4?

  5. 5

    How to Debug Custom Devise Strategy in Rails 4?

  6. 6

    Updating custom field for devise user model in Rails 4

  7. 7

    Rails - Devise - Add action to controller

  8. 8

    How to generate Registration controller of devise gem

  9. 9

    Rails 4: how to access the content of Devise Registrations Controller

  10. 10

    Rails 4 Devise custom routes

  11. 11

    Rails 4 Devise custom routes

  12. 12

    Having trouble adding a "custom" field to a Devise registration form

  13. 13

    Rails, use a form field to set something on the user registration (devise)

  14. 14

    Devise sign up - how to save an attribute without having a form field for it (Rails4, devise)

  15. 15

    Ruby on Rails: How to customise error message for MailForm when field is blank

  16. 16

    Add a Time column to Devise and have it updated manually from controller in Rails 4

  17. 17

    Rails 4 - devise_for not using custom controllers

  18. 18

    Rails 4 + Custom Devise attributes -- ParameterSanitizer Error

  19. 19

    Add Custom Type to Rails User Registration

  20. 20

    Unable to add custom fields to my Devise user model in Rails 4 with strong parameters

  21. 21

    Unable to add custom fields to my Devise user model in Rails 4 with strong parameters

  22. 22

    Yii - How to add custom field to form that can be accesed in controller?

  23. 23

    How to add a custom route, controller, and action in Ruby on Rails?

  24. 24

    How to setup angular-rails 4.2 - devise registration/authentication?

  25. 25

    How do I validate certain fields with rails devise on registration only

  26. 26

    In Rails 4, how can I get data out of a devise user within a controller?

  27. 27

    How do I use objects in controller for viewing data in Rails 4 devise sign_in page

  28. 28

    Devise rails 4 - Can't edit my new.html.erb file under devise registration

  29. 29

    Proper way of rails routing custom Users controller and devise gem

HotTag

Archive