# Creating an account with a username in devise

**URL:** <https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503>\
**Category:** Rails\
**Created:** [June 25, 2013, 11:06pm UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503 "2013-06-25T23:06:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [June 25, 2013, 11:06pm UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/1 "2013-06-25T23:06:16Z")

</div>

Hello, all. I have followed these links and have enabled a (current)  
user to login with a username:

> **[\#209 Introducing Devise - RailsCasts](http://railscasts.com/episodes/209-introducing-devise)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#210 Customizing Devise - RailsCasts](http://railscasts.com/episodes/210-customizing-devise)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[How To: Allow users to sign in using their username or email address](https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address)**
>
> Flexible authentication solution for Rails with Warden. - heartcombo/devise

I want to allow a user to sign up with a username but am getting these  
errors and don’t know how to proceed. Notice the ‘Email can’t be blank’  
error, even though I’m not wanting to accept an email for  
authentication. It also doesn’t recognize that I entered a password or  
that they matched (even though they did):

Sign up  
3 errors prohibited this user from being saved:  
Email can’t be blank  
Password can’t be blank  
Password confirmation doesn’t match Password

Please see attached devise view new.html.erb and devise model user.rb

Please let me know if you require any further information. Any help  
would be greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![olveram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aeb1de/32.png) [@olveram](https://www.ruby-forum.com/u/olveram)\
**Post date:** [June 26, 2013, 12:13am UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/2 "2013-06-26T00:13:22Z")

</div>

On Tue, Jun 25, 2013 at 5:06 PM, monte olvera [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I want to allow a user to sign up with a username but am getting these  
> errors and don’t know how to proceed. Notice the ‘Email can’t be blank’  
> error, even though I’m not wanting to accept an email for  
> authentication.

Did you remember to set “config.authentication\_keys = [:username]” in  
the initializer? Show us at least the non-commented-out lines.

> It also doesn’t recognize that I entered a password or  
> that they matched (even though they did):

That sounds like you may be pulling out the wrong part of the params.  
Can you show us the controller action and logged params? Or are you  
just using the default Devise controllers?

-Dave

–  
Dave A., the T. Rex of Codosaurus LLC,  
secret-cleared freelance software developer  
taking contracts in or near NoVa or remote.  
See information at [http://www.Codosaur.us/](http://www.Codosaur.us/).

---

<div class="post-metadata">

**Author:** ![olveram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aeb1de/32.png) [@olveram](https://www.ruby-forum.com/u/olveram)\
**Post date:** [June 26, 2013, 1:48am UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/3 "2013-06-26T01:48:09Z")

</div>

> Did you remember to set “config.authentication\_keys = [:username]” in  
> the initializer? Show us at least the non-commented-out lines.  
> $ grep -v # config/initializers/devise.rb |grep .  
> Devise.setup do |config|  
> config.mailer\_sender = “[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
> require ‘devise/orm/active\_record’  
> config.case\_insensitive\_keys = [:username]  
> config.strip\_whitespace\_keys = [:username]  
> config.stretches = Rails.env.test? ? 1 : 10  
> config.use\_salt\_as\_remember\_token = true  
> config.timeout\_in = 30.minutes  
> config.reset\_password\_within = 2.hours  
> config.sign\_out\_via = :get  
> config.authentication\_keys = [:username]  
> config.reset\_password\_keys = [:username]  
> config.confirmation\_keys = [:username]  
> end

> That sounds like you may be pulling out the wrong part of the params.  
> Can you show us the controller action and logged params? Or are you  
> just using the default Devise controllers?

I apologize if I’m not following you, because I’m new to Ruby on Rails,  
but Devise didn’t install any Controller, nor did it add anything to the  
default app/controller/application\_controller.rb. So, I assume I’m  
using the default. However I did add an entry to my application’s  
controller so I could authenticate before going to any page:

$ cat app/controllers/my\_app\_controller.rb  
class MyAppController \< ApplicationController  
before\_filter :authenticate\_user!  
…

When I was looking through the logs (to respond to your “logged params”  
question I found this error:  
“Started POST “/users” for 127.0.0.1 at 2013-06-25 19:29:41 -0400  
Processing by Devise::RegistrationsController#create as HTML  
Parameters: {“utf8”=\>”✓“,  
“authenticity\_token”=\>“tltbt2w5FkZQbkbLqYaZIPiqk3J7VsdQIvKWFDCYLAs=”,  
“user”=\>{“firstname”=\>“bob”, “lastname”=\>“smith”,  
“password”=\>”[FILTERED]“, “password\_confirmation”=\>”[FILTERED]“},  
“commit”=\>“Sign up”}  
WARNING: Can’t mass-assign protected attributes for User: password”

So, I googled it and saw references to removing the “attr\_protected  
:password” from the model (user.rb).

I did that and now only get the “Email can’t be blank” error when trying  
to sign up a user.

---

<div class="post-metadata">

**Author:** ![olveram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aeb1de/32.png) [@olveram](https://www.ruby-forum.com/u/olveram)\
**Post date:** [June 26, 2013, 2:30am UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/4 "2013-06-26T02:30:41Z")

</div>

On Tue, Jun 25, 2013 at 7:48 PM, monte olvera [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Did you remember to set “config.authentication\_keys = [:username]” in  
> > the initializer? Show us at least the non-commented-out lines.

> $ grep -v # config/initializers/devise.rb |grep .

Ah, we’re obviously not dealing with a total programming-newbie here!  
🙂

> config.case\_insensitive\_keys = [:username]  
> config.strip\_whitespace\_keys = [:username]  
> …  
> config.authentication\_keys = [:username]  
> config.reset\_password\_keys = [:username]  
> config.confirmation\_keys = [:username]

Okay, so far so good. Just to be sure, I compared it with one of my  
apps that uses username to log in, and the only other mention of  
username I had was config.unlock\_keys (which you might not be using at  
all).

> > That sounds like you may be pulling out the wrong part of the params.  
> > Can you show us the controller action and logged params? Or are you  
> > just using the default Devise controllers?
> 
> I apologize if I’m not following you, because I’m new to Ruby on Rails,  
> but Devise didn’t install any Controller,

Actually, it does, just not placed in with your own controllers. In  
its own directory, it has controllers and views and so on. Gems  
provide Rails additional places to find such things. Some, such as  
Devise, also let you install stuff into your app’s code dirs so that  
you can mess with them. (I’m guessing you did that with its views, in  
order to make it ask for first and last name instead of email.) Some  
don’t, and are meant to run strictly out of their own dirs (modulo the  
configuration initializer).

> I assume I’m using the default.

I’d agree.

> However I did add an entry to my application’s  
> controller so I could authenticate before going to any page:
> 
> $ cat app/controllers/my\_app\_controller.rb  
> class MyAppController \< ApplicationController  
> before\_filter :authenticate\_user!

That should be fine.

> When I was looking through the logs (to respond to your “logged params”  
> question I found this error:

The rest of it looks good, confirming you’re sending the right stuff  
to the right place.

> WARNING: Can’t mass-assign protected attributes for User: password"
> 
> So, I googled it and saw references to removing the “attr\_protected  
> :password” from the model (user.rb).

Yes, that’s fairly important to understand. Another idea, though, is  
to get a bit ahead of the curve, and use Rails 4 with its “strong  
parameters”. 4 was just released today, so it’s now officially stable  
so you can build with it confidently. Also, if you’re trying to get  
actual paid work in it, it’s new enough that the competition will have  
some struggles too, which evens the playing field a bit against  
grizzled veterans like me. Oh, wait, never mind, stick with Rails 3,  
better yet go check out Rails 2, pay no attention to the release no  
longer behind the curtain… 😉

> I did that and now only get the “Email can’t be blank”  
> error when trying to sign up a user.

Ah, good progress.

Maybe there’s still a validation looking for email in your User class?  
Can you post that?

You could also go on a “search and destroy” mission: find all mentions  
of email in your app and examine them to see if they could be the  
cause. (Do you know about the “ack” utility?)

Come to think of it, there’s a much simpler possibility; it may sound  
stupid, but we’ve all done it. Maybe you didn’t restart your Rails  
server after modifying Devise. You need to do that to make it reread  
the initializers, such as the one that tells Devise what fields to use  
as keys.

-Dave

–  
Dave A., the T. Rex of Codosaurus LLC,  
secret-cleared freelance software developer  
taking contracts in or near NoVa or remote.  
See information at [http://www.Codosaur.us/](http://www.Codosaur.us/).

---

<div class="post-metadata">

**Author:** ![olveram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aeb1de/32.png) [@olveram](https://www.ruby-forum.com/u/olveram)\
**Post date:** [June 26, 2013, 3:11am UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/5 "2013-06-26T03:11:33Z")

</div>

first of all, thanks man for helping me, I really appreciate it!

> Ah, we’re obviously not dealing with a total programming-newbie here!  
> 🙂

🙂 I said I was new to Ruby, not to programming. Started with Qbasic  
on my commodore back in 1995 when I was 10. I know Assembly, C/C++,  
Python, Shell, PHP. But I digress…

> Actually, it does, just not placed in with your own controllers. In  
> its own directory, it has controllers and views and so on. Gems  
> provide Rails additional places to find such things. Some, such as  
> Devise, also let you install stuff into your app’s code dirs so that  
> you can mess with them.

Here’s what I ran to install devise:  
rails generate devise:install  
rails generate devise user  
rails generate devise:views

> (I’m guessing you did that with its views, in  
> order to make it ask for first and last name instead of email.)

Nope, I edited that myself because I couldn’t find anything anywhere.

> parameters". 4 was just released today

I am using rails 4:  
$ rails -v  
Rails 4.0.0.rc2

> Also, if you’re trying to get actual paid work in it

Ha! I better not be, my resume would read: “I googled stuff!”

Actually I want to get this (personal) project done and figured I’d go  
back and learn why I did what I did and why it worked later. This goes  
against everything in me but I simply don’t have the time (right now) to  
dedicate a month to fully learning ruby on rails before getting this  
project done.

> Maybe there’s still a validation looking for email in your User class?  
> Can you post that?

$ grep -v # app/models/user.rb |grep .  
class User \< ActiveRecord::Base  
devise :database\_authenticatable, :registerable, :recoverable,  
:rememberable, :trackable, :validatable, :authentication\_keys =\>  
[:username]  
validates\_presence\_of :firstname, :lastname, :unique =\> true  
after\_validation :createUsername  
attr\_accessible :username, :password, :password\_confirmation,  
:remember\_me, :firstname, :lastname  
def createUsername  
firstnamePart=self.firstname[0,1].downcase  
lastnamePart=self.lastname[0,5].downcase  
username=lastnamePart + firstnamePart  
count=0  
while username.length != 7  
username=username + count.to\_s  
count +=1  
end  
self.username=username  
end  
def self.find\_first\_by\_auth\_conditions(warden\_conditions)  
conditions = warden\_conditions.dup  
if login = conditions.delete(:login)  
where(conditions).where([“lower(username) = :value OR  
lower(email) = :value”, { :value =\> login.downcase }]).first  
else  
where(conditions).first  
end  
end  
end

> You could also go on a “search and destroy” mission: find all mentions  
> of email in your app and examine them to see if they could be the  
> cause.

$ grep -ri email \*|egrep -v “#|log”  
app/views/devise/mailer/unlock\_instructions.html.erb:

Hello \<%=  
@resource.email %\>!

  
app/views/devise/mailer/confirmation\_instructions.html.erb:

Welcome  
\<%= @resource.email %\>!

  
app/views/devise/mailer/reset\_password\_instructions.html.erb:

Hello  
\<%= @resource.email %\>!

  
app/views/devise/mailer/reset\_password\_instructions.html.erb:

If you  
didn’t request this, please ignore this email.

  
config/locales/devise.en.yml: send\_instructions: ‘You will receive  
an email with instructions about how to reset your password in a few  
minutes.’  
config/locales/devise.en.yml: send\_instructions: ‘You will receive  
an email with instructions about how to confirm your account in a few  
minutes.’  
config/locales/devise.en.yml: send\_paranoid\_instructions: ‘If your  
e-mail exists on our database, you will receive an email with  
instructions about how to confirm your account in a few minutes.’  
config/locales/devise.en.yml: send\_instructions: ‘You will receive  
an email with instructions about how to unlock your account in a few  
minutes.’  
config/locales/devise.en.yml: send\_paranoid\_instructions: ‘If your  
account exists, you will receive an email with instructions about how to  
unlock it in a few minutes.’  
Binary file db/development.sqlite3 matches  
db/schema.rb: t.string “email”,  
default: “”, null: false  
db/schema.rb: add\_index “users”, [“email”], name:  
“index\_users\_on\_email”, unique: true  
Binary file  
tmp/cache/assets/development/sprockets/d585a06e2ee6203ccb04c8b84150d14d  
matches  
Binary file  
tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705  
matches  
Binary file  
tmp/cache/assets/development/sprockets/8c2b061e379a23e7c4d207adcf992462  
matches  
Binary file  
tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953  
matches  
Binary file  
tmp/cache/assets/development/sprockets/b7fc7a50cc3464d16f378714639f14e2  
matches

> (Do you know about the “ack” utility?)  
> a rails utility? Nope, never heard of an ‘ack’ utility.

> Maybe you didn’t restart your Rails server after modifying Devise.  
> I’ve only done that 500 times. Really, after every single change I made  
> I restarted rack, just in case.

---

<div class="post-metadata">

**Author:** ![olveram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aeb1de/32.png) [@olveram](https://www.ruby-forum.com/u/olveram)\
**Post date:** [June 26, 2013, 1:53am UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/6 "2013-06-26T01:53:09Z")

</div>

Here are the logs I get from rack when submitting the ‘sign up’ form:

Started POST “/users” for 127.0.0.1 at 2013-06-25 19:50:18 -0400  
Processing by Devise::RegistrationsController#create as HTML  
Parameters: {“utf8”=\>“✓”,  
“authenticity\_token”=\>“tltbt2w5FkZQbkbLqYaZIPiqk3J7VsdQIvKWFDCYLAs=”,  
“user”=\>{“firstname”=\>“bob”, “lastname”=\>“smith”,  
“password”=\>"[FILTERED]", “password\_confirmation”=\>"[FILTERED]"},  
“commit”=\>“Sign up”}  
(0.1ms) begin transaction  
(0.1ms) rollback transaction  
Rendered devise/shared/\_links.erb (0.3ms)  
Rendered devise/registrations/new.html.erb within layouts/application  
(3.6ms)  
Completed 200 OK in 97ms (Views: 10.8ms | ActiveRecord: 0.2ms)

Started GET “/assets/application.css?body=1” for 127.0.0.1 at 2013-06-25  
19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/bootstrap\_and\_overrides.css?body=1” for 127.0.0.1  
at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/jquery.js?body=1” for 127.0.0.1 at 2013-06-25  
19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/jquery\_ujs.js?body=1” for 127.0.0.1 at 2013-06-25  
19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-transition.js?body=1”  
for 127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-alert.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-modal.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-dropdown.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-scrollspy.js?body=1”  
for 127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-tab.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-tooltip.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-popover.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-button.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-carousel.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-collapse.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-typeahead.js?body=1”  
for 127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap/bootstrap-affix.js?body=1” for  
127.0.0.1 at 2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/twitter/bootstrap.js?body=1” for 127.0.0.1 at  
2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/turbolinks.js?body=1” for 127.0.0.1 at 2013-06-25  
19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/bootstrap.js?body=1” for 127.0.0.1 at 2013-06-25  
19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/prayer\_requests.js?body=1” for 127.0.0.1 at  
2013-06-25 19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

Started GET “/assets/application.js?body=1” for 127.0.0.1 at 2013-06-25  
19:50:18 -0400  
[2013-06-25 19:50:18] WARN Could not determine content-length of  
response body. Set content-length of the response or set  
Response#chunked = true

---

<div class="post-metadata">

**Author:** ![olveram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aeb1de/32.png) [@olveram](https://www.ruby-forum.com/u/olveram)\
**Post date:** [June 26, 2013, 3:20am UTC](https://www.ruby-forum.com/t/creating-an-account-with-a-username-in-devise/235503/7 "2013-06-26T03:20:07Z")

</div>

that ‘email’ reference search looked lousy posting it directly, so here  
it is in an attached file.
