# Restful Authentication routes

**URL:** <https://www.ruby-forum.com/t/restful-authentication-routes/87981>\
**Category:** Rails\
**Created:** [March 7, 2007, 10:09am UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981 "2007-03-07T10:09:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 10:09am UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/1 "2007-03-07T10:09:35Z")

</div>

Hi all,

I’ve been messing with Restful Authentication on Rails 1.2.2. I’m  
having trouble setting up the routes. In routes.rb I have:

map.resources :users  
map.resources :sessions

As per the documentation.

However, I need to add routes for:

1. The action ‘destroy’ in the sessions controller - that’s simple  
enough.

2. The action ‘activate’ in the users controller - with the  
activation\_code parameter passed in too.

Can anyone explain how to do #2?

Bonus question - should I be using singleton resources, as mentioned  
in the comments on:

[http://agilewebdevelopment.com/plugins/restful\_authentication](http://agilewebdevelopment.com/plugins/restful_authentication)

Thanks a lot,

Tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/2 "2007-03-07T12:28:06Z")

</div>

I’m stuck here too…no mention of this in documentation (that I could  
see). When I click on the activate link in the activation email it  
errors “no route found to match “/activate/d8e1326e13c18a” with  
{:method=\>:get}”

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/3 "2007-03-07T18:07:12Z")

</div>

Will /logout still require a DELETE method this way or will it accept  
GET?

RSL

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/4 "2007-03-07T18:20:18Z")

</div>

> 1. The action ‘activate’ in the users controller - with the  
> activation\_code parameter passed in too.

I have this route to handle activation:

map.activate ‘/activate/:id’, :controller =\> ‘users’, :action =\>  
‘activate’

And these are some other routes that I find useful:

map.reactivate ‘/reactivate/:id’, :controller =\> ‘users’, :action =\>  
‘reactivate’  
map.lostpassword ‘/lostpassword’, :controller =\> ‘users’, :action =\>  
‘lostpassword’  
map.resetpassword ‘/resetpassword’, :controller =\> ‘users’, :action  
=\> ‘resetpassword’

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/5 "2007-03-07T17:56:10Z")

</div>

> 1. The action ‘destroy’ in the sessions controller - that’s simple  
> enough.

map.connect ‘logout’, :controller =\> ‘sessions’, :action =\> ‘destroy’

> 1. The action ‘activate’ in the users controller - with the  
> activation\_code parameter passed in too.

# use :code, :token, :id, :foobarbaz, whatever you like

map.connect ‘activate/:code’, :controller =\> ‘users’, :action =\>  
‘activate’

> Bonus question - should I be using singleton resources, as mentioned  
> in the comments on:

The last time the plugin was updated was before singleton resources.  
Please do send in a nice patch if you’d like to see singleton  
resources in the restful auth plugin.

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/6 "2007-03-07T18:32:23Z")

</div>

On 3/7/07, Russell N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Will /logout still require a DELETE method this way or will it accept GET?

The Session#Destroy in its default state accepts a get. i.e. /logout.

It’s not destroying an Active Record object, just the session, so I  
think it’s a useful feature. Note that restful\_authenttication doesn’t  
have a User#Destroy method- that would certainly need a delete method.

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/7 "2007-03-07T18:48:24Z")

</div>

That makes sense. I decided to try it for myself and as I was writing it  
I  
realized that my new logout\_url wouldn’t be part of the map.resources  
and  
thus not bound by its methods. Heh.

RSL

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 8, 2007, 2:24pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/8 "2007-03-08T14:24:59Z")

</div>

On Mar 7, 4:55 pm, “Rick O.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 1. The action ‘activate’ in the users controller - with the  
> > activation\_code parameter passed in too.
> 
> # use :code, :token, :id, :foobarbaz, whatever you like
> 
> map.connect ‘activate/:code’, :controller =\> ‘users’, :action =\> ‘activate’

I ended up doing:

map.resources :users, :collection =\> { :activate =\> :any }

I figured it was a little more… RESTful!

> The last time the plugin was updated was before singleton resources.  
> Please do send in a nice patch if you’d like to see singleton  
> resources in the restful auth plugin.

I’ve just been through and converted it all, so I’ll throw something  
together now.

Cheers,

Tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 7, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/restful-authentication-routes/87981/9 "2007-03-07T18:23:19Z")

</div>

Okay guys,

first of all, I’ve hacked the restful authentication stuff to suit my  
tastes better. I’ll give you the short answer first and then explain  
it a bit:

map.resource :account  
map.account\_activation ‘/account/activate/:activation\_code’,  
:controller =\> ‘account’, :action =\> ‘activate’

Then to generate the the URL for activation I call:

account\_activation\_url(:activation\_code =\> user.activation\_code)

A couple of things to note:

1 - My model is called User  
2 - My end-user-visible signup and preferences/password management is  
done through AccountController which is a _singleton_ because users  
can’t see other users and you can only have one account.  
3 - Administrators _can_ see other users (plus extra data users can’t  
see about themselves) so for that purpose they have a UsersController

Hopefully that explains why I chose :account as a singleton rather  
than a collection.

And, that should also illustrate that yes, your :session resource is  
_also_ a singleton - each HTTP session can only have one logical  
session so it’s a singleton.

HTH,  
Trevor
