# Restful rails: when simple just isn't enough

**URL:** <https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324>\
**Category:** Rails\
**Created:** [November 26, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324 "2006-11-26T23:50:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 26, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/1 "2006-11-26T23:50:23Z")

</div>

Hi, I’ve read the blogs, I’ve seen the Peepcode screencast, and I even  
read the beta book from the Pragmatic guys - still, I fail to understand  
how REST can simplify my controllers beyond the CRUD operations.

Example: I got a simple article object. I got want to get the most  
recent articles, that’s not supported by the CRUD stuff, so I add:

map.resources :articles, :collection =\> { :recent =\> :get }

… in my routes. Now say I want to flag some of those articles as  
‘enabled/disabled’ or ‘featured/not featured’:

map.resources :articles, :member =\> { :featured =\> :put, :enabled =\>  
:put }

I can think of about 39303049 different ‘methods’ I need to call in this  
fashion. Does it mean I have to add that many ‘custom’ resource routes  
to my route file, just to then have to declare them as part of my  
controllers? In which case, short if simplifying the CRUD action, REST  
didn’t help simplify my controller at all?

I know I’m missing something, the question is, what 😃 If you guys  
could point me in the right direction, that would be great…

---

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 27, 2006, 1:26am UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/2 "2006-11-27T01:26:16Z")

</div>

On Nov 26, 2006, at 4:50 PM, Steve D. wrote:

> … in my routes. Now say I want to flag some of those articles as  
> ‘enabled/disabled’ or ‘featured/not featured’:
> 
> map.resources :articles, :member =\> { :featured =\> :put, :enabled =\>  
> :put }  
> [snip]

Okay, first up is a disclaimer. I’m still getting my feet wet with  
REST so this might be totally wrong. Proceed at your own risk.

When looking at your collection from these other perspectives (recent  
= time, status = enabled/disabled/featured/etc), these other  
perspectives become controllers.

GET [example.com/recent](http://example.com/recent)  
This returns some default timespan of recent articles.

GET [example.com/recent/45](http://example.com/recent/45)  
This returns articles from the past 45 days.

Or maybe you just take additional parameters in to your regular #show  
method.  
GET [example.com/articles?begin=today&end=45](http://example.com/articles?begin=today&end=45)  
This might behave just like the #recent/45 example above.

I highly recommend looking at the articles on wikipedia [1] and  
RestWiki [2] to understand how to apply REST principles. The REST  
approach will likely result in more controllers, but each controller  
will be much simpler to implement and test. If you are still stuck in  
the mode that says there is a 1:1 relationship between controllers  
and models then you might be in for a rough ride.

cr

[1] [Representational state transfer - Wikipedia](http://en.wikipedia.org/wiki/Representational_State_Transfer)  
[2] [http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage](http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage)

---

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 27, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/3 "2006-11-27T12:57:09Z")

</div>

Thank you!

Can anyone confirm Chuck’s highlights? I get this feeling RESTful  
application are still very much bleeding edge…

---

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 27, 2006, 1:13pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/4 "2006-11-27T13:13:25Z")

</div>

Steve D. wrote:

> Thank you!
> 
> Can anyone confirm Chuck’s highlights? I get this feeling RESTful  
> application are still very much bleeding edge…

Like Chuck, I’m no REST expert, but am halfway through refactoring an  
app to be RESTful, and it’s improving it no end. The structure is  
stronger, the reasoning better, and the whole thing is a lot less messy.

In all my RESTful controllers I’m set up the :index action to display  
the 20 most recent items, and if I need to filter it, perhaps by  
restricting them to items that are tagged with “foo”, I simply pass a  
:tag =\> “foo” parameter along with the request. It’s trivial if there’s  
just one possible filter, not that much more difficult using case…when  
with using more than one possible one. I’ve only needed to resort to  
additional actions in a couple of controllers (and it’s a fairly big  
app), and in those it’s been for specific destructive actions, e.g.  
disapproving an item.

HTH  
CT

---

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 27, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/5 "2006-11-27T20:40:32Z")

</div>

On 11/26/06, Steve D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to my route file, just to then have to declare them as part of my  
> controllers? In which case, short if simplifying the CRUD action, REST  
> didn’t help simplify my controller at all?

Could you not just pass these “methods” during a post update? These  
‘methods’ really sound like state information about the object that need  
to  
be updated. It sounds overkill and redundant to add specialized  
methods  
for performing simple state updates.

If you want to display or filter the displayed result by certain state  
information you can pass them as parameters to either the index function  
[http://example.com/articles?enabled=true](http://example.com/articles?enabled=true), etc. Alternatively if you wish  
you  
could create a custom :member function in the routes when you wish to  
perform very selective filtering. Ie. recent 45 days of articles, etc.

Rest works well in some cases but possibly not in every case. If its  
causing you more grief than its worth than don’t bother. I’ve started  
developing a ‘rest’ app and I must admit that it does simplify my life  
and  
removes a lot of the redundant code/views.

Adam

---

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 28, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/6 "2006-11-28T13:06:46Z")

</div>

Your collection methods are fine - think of collection actions as  
alternative GET requests. This is still the R part of CRUD. You may  
want to consider using query string parameters instead though.

For your update methods - these could all be part of a single update  
operation.

---

<div class="post-metadata">

**Author:** ![stevedogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@stevedogers](https://www.ruby-forum.com/u/stevedogers)\
**Post date:** [November 27, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/restful-rails-when-simple-just-isnt-enough/77324/7 "2006-11-27T21:44:41Z")

</div>

On 11/27/06, Adam R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > fashion. Does it mean I have to add that many ‘custom’ resource routes  
> > to my route file, just to then have to declare them as part of my  
> > controllers? In which case, short if simplifying the CRUD action, REST  
> > didn’t help simplify my controller at all?
> 
> Could you not just pass these “methods” during a post update? These  
> ‘methods’ really sound like state information about the object that need to  
> be updated. It sounds overkill and redundant to add specialized methods  
> for performing simple state updates.

Not only could he, but that’s what he SHOULD do. He’s simply updating  
the attributes of a resource, which is what HTTP PUT is for.

Pat
