# Working around/with Restful Authentication

**URL:** <https://www.ruby-forum.com/t/working-around-with-restful-authentication/196729>\
**Category:** Rails\
**Created:** [June 29, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/working-around-with-restful-authentication/196729 "2008-06-29T15:57:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nik\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@Nik\_B](https://www.ruby-forum.com/u/Nik_B)\
**Post date:** [June 29, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/working-around-with-restful-authentication/196729/1 "2008-06-29T15:57:03Z")

</div>

I’m using Restful Authentication, and the code to create a user is  
pretty straight forward - there is a before\_save action and a  
before\_create action:

before\_save :encrypt\_password  
before\_create :make\_activation\_code

But for some reason when I try to create a user programmatically in  
the controller like this:

User.new(:email =\> ‘removed\_email\_address@domain.invalid’, :password =\>  
‘123456’, :password\_confirmation =\> ‘123456’)

Nothing happens. I get a user that I can’t save:

> u = User.new(:email =\> ‘removed\_email\_address@domain.invalid’, :password =\> ‘123456’, :password\_confirmation =\> ‘123456’)  
> =\> #\<User id: nil, login: nil, email: “removed\_email\_address@domain.invalid”,  
> crypted\_password: nil, salt: nil, created\_at: nil, updated\_at: nil,  
> remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
> nil, activated\_at: nil, is\_seller: false\>
> 
> > u.save  
> > =\> false

Any clues as to how I could create a user without actually being sent  
to the user controller’s create action? The before\_save and  
before\_create actions are private.

Thanks,  
Nik

---

<div class="post-metadata">

**Author:** ![Nik\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@Nik\_B](https://www.ruby-forum.com/u/Nik_B)\
**Post date:** [June 29, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/working-around-with-restful-authentication/196729/2 "2008-06-29T16:14:36Z")

</div>

Nik,

The default generator for restful\_authentication creates a user model  
with the constraint “validates\_presence\_of :login”. Unless you  
removed this from the user model you’ll need to supply a login.

Also, did you check the errors on the user model after the failed save  
to see why it’s not saving? u.errors (using your example) will give  
you the list of errors.

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![Nik\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@Nik\_B](https://www.ruby-forum.com/u/Nik_B)\
**Post date:** [June 29, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/working-around-with-restful-authentication/196729/3 "2008-06-29T16:19:17Z")

</div>

Thanks mike - I did remove this constraint… but I did not remove the  
constraint that login must be unique… so it was conflicting with a  
previous user that has a login of ’ ’

---

<div class="post-metadata">

**Author:** ![Nik\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@Nik\_B](https://www.ruby-forum.com/u/Nik_B)\
**Post date:** [June 29, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/working-around-with-restful-authentication/196729/4 "2008-06-29T16:27:30Z")

</div>

Next time use #save! rather than #save to get validation errors.  
Alternatively you can also view the errors on the object by calling  
#errors and looking at what’s in there (e.g., puts user.errors).

–Jeremy

On Sun, Jun 29, 2008 at 9:18 AM, Nik B [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > removed this from the user model you’ll need to supply a login.
> > 
> > > nil, activated\_at: nil, is\_seller: false\>

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com](http://entp.com)

Read my books:  
Ruby in Practice ([Ruby in Practice](http://manning.com/mcanally/))  
My free Ruby e-book ([http://humblelittlerubybook.com/](http://humblelittlerubybook.com/))

Or, my blogs:

> **[Site not found · DreamHost](http://mrneighborly.com)**
>
> The owner of this domain has not yet uploaded their website.

[http://rubyinpractice.com](http://rubyinpractice.com)
