Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add fields to User model in Devise

I'm a bit of a newbie at Rails and feel I'm missing a trick here. I'm trying to add a phone_number field to my Devise-generated User model, but I'm having an issue with saving it. I've done the rails generate devise:views, updated the edit.html.erb file to add in the :phone_number field, and created a migration to add the phone_number field to the model. It's not saving to the model because (as I understand it) I can't update the controller to include the new fields.

Do I need to create an app/controllers/users/registration_controller.rb defined with class Users::RegistrationsController < Devise::RegistrationsController and then monkey patch the update method? Or is there a more straightforward/elegant/easier way?

I realize there are a couple other questions related to this on the site, but one offers no useful answers, and the other simply details what I mention here. Is there anything more to it?

Thanks.

like image 265
Kevin Pope Avatar asked Dec 13 '10 09:12

Kevin Pope


People also ask

How do I add a column in devise gem?

When you create a User model with Devise using the command rails generate devise User . Devise will create default columns in the database for your User model. To add your own columns for your users you can create a new Rails migration and add any columns you want for your users.


1 Answers

After you add the field to your database through a migration you will also need to add it to your list of accessible attributes in your User model. Your attr_accessible list should look something like the following depending on what devise modules you are using.

attr_accessible :email, :password, :password_confirmation, :phone_number
like image 110
Braden Becker Avatar answered Sep 23 '22 00:09

Braden Becker