# Can all methods be made to fit REST?

**URL:** <https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628>\
**Category:** Rails\
**Created:** [October 18, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628 "2007-10-18T19:31:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/1 "2007-10-18T19:31:49Z")

</div>

I know that if you listen to DHH and similar REST evangelists that  
they would have you believe that all methods can be reduced to the  
seven that fit the REST conventions (create, show, update, destroy,  
index, new, and edit). I am not yet convinced.

For example can anybody think what model I would use for  
forgot\_password and reset\_password. I have a user model that has a  
password as a field. I do not really want to have a password model  
since I guess this is supposed to map to a password table in the  
database.

What does everybody think. As you can see this is more of a  
philosophical than a technical question.  
Looking forward to some interesting replies.

Anthony

---

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/2 "2007-10-18T19:44:06Z")

</div>

Would this not be an update to the user model? That is, changing a  
password would be as simple as updating the user model.

However, lost password recovery is something else. From my  
understanding, REST is not only a way to design web sites that are  
then easier to use as an API, but to allow others to put their web  
front-end on top of your RESTful database back-end. If this is the  
case, wouldn’t lost password recovery be something that the front-end  
might do, given the basic primitives you provide?

To be honest, REST not something I am following closely, but I do  
understand the need for a unique operation at a unique address. No  
more index.php-does-everything for me 🙂

–Michael

---

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/3 "2007-10-18T19:44:41Z")

</div>

On Oct 18, 2007, at 12:29 , Ant wrote:

> For example can anybody think what model I would use for  
> forgot\_password and reset\_password. I have a user model that has a  
> password as a field. I do not really want to have a password model  
> since I guess this is supposed to map to a password table in the  
> database.

There’s no reason to have a one-to-one correspondence between  
controllers and models, nor between models and database tables. I’ve  
used a separate AuthenticationsController (with new, create, and  
destroy actions) to handle logins. Works well.

Forgotten/reset password, depending on what you’re doing, you might  
have a ForgottenPasswordNotificationController with new and create  
actions to send out notifications, and a separate PasswordsController  
(perhaps nested under your UsersController?) that has edit and update  
actions? Or perhaps PasswordsController#new posts to #create which in  
turn sends the notification? Just ideas here, and I haven’t really  
thought through them too far, but it’s a start. The thing to keep in  
mind is the resource that you’re acting on.

Hope this helps. There’s no reason you have to map everything to  
REST, but many times you can if you want.

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/4 "2007-10-18T19:49:23Z")

</div>

In my experience, I’ve found that you either create a custom method  
(blahblahblahdon’tdothatblahblah) or another controller. In this  
situation, it would depend on the scale of the application and how  
much the remote API was going to be used. Little to no remote API  
usage? Custom method it is. A _lot_ of API usage…I might consider  
another controller.

I don’t think either way is the absolute correct way, but I think you  
have to formulate your answer based on the application itself and how  
it will be used.

–Jeremy

On 10/18/07, Ant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> database.

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

My books:  
Ruby in Practice

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book

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

My blogs:

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

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

---

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/5 "2007-10-18T20:21:34Z")

</div>

Ant wrote:

> What does everybody think. As you can see this is more of a  
> philosophical than a technical question.  
> Looking forward to some interesting replies.
> 
> Anthony

I would create a PasswordRemindersController.

```
* 'new' action provides a form for the user to enter their username
  and/or email, or something.
* 'create' action sends them an email with a reset password and
  redirects to 'show' action.
* 'show' action simply gives a message that the email has been sent
  and that they should check for it.

```

The ‘show’ action could be eliminated if you wanted the ‘create’ to just  
show the message, but convention dictates that POST and PUTS redirect on  
success, otherwise if someone started reloading that page, it would keep  
resetting their password to something else and sending another email.

As Michael pointed out, there is absolutely no need for a 1 to 1  
correlation between controllers -\> models -\> tables. This is a classic  
example of a RESTful controller with no need for a corresponding model.

---

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/6 "2007-10-18T20:19:14Z")

</div>

On Oct 18, 12:29 pm, Ant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For example can anybody think what model I would use for  
> forgot\_password and reset\_password. I have a user model that has a  
> password as a field. I do not really want to have a password model  
> since I guess this is supposed to map to a password table in the  
> database.

Putting on my “Everything can be restfully modeled” hat, what about  
this?

map.resources :forgotten\_passwords

The new form would have a field for email address. Create would call  
logic in User to do the reset. The link would be sent to the user,  
using the reset code as the ID for forgotten\_passwords

[http://app.com/forgotten\_passwords/123456asswqdwqj1221312321](http://app.com/forgotten_passwords/123456asswqdwqj1221312321)

The show on the forgotten password would have a form to put to the  
user resource and change the pass.

Actually, now that I say all that, it doesn’t seem nearly as crazy as  
I thought it would. That’s pretty clean.

---

<div class="post-metadata">

**Author:** ![ant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ant](https://www.ruby-forum.com/u/ant)\
**Post date:** [October 18, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/can-all-methods-be-made-to-fit-rest/117628/7 "2007-10-18T21:05:36Z")

</div>

Thank you all for your replies. All very helpful and have got me to  
reassess both MVC and REST again. I think that taken together there  
will be some stuff I can implement, particularly not having to have a  
model for every controller.

Thanks again,

Anthony
