# Asking another way - no rest actions in a restful controller

**URL:** <https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972>\
**Category:** Rails\
**Created:** [October 4, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972 "2006-10-04T12:36:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 4, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/1 "2006-10-04T12:36:14Z")

</div>

My previous post seems to dead , aka, no replies. Thought I’d take a  
broader approach.  
I have a restful controller (created via the scaffold\_resources in  
edgerails). I want to create a search function. While I added the  
search  
function into the controller, things seem to by pass my ‘def  
search…’. Is this normal for a rest controller and can anyone  
suggest a better way ?

TIA  
Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 4, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/2 "2006-10-04T16:34:37Z")

</div>

My first questions are, “what is the functionality you’re trying to  
create?” and, “do you need a search(es) controller?”

I’m building an app right now that is a search-type tool. I have three  
different kinds of resources I use: Query (base class), Search and  
AdvancedSearch (both inherit from Query). So then I’ve mapped the  
resources in routes.rb.

Likely, you’re not creating models for your searches as I have, but I  
wouldn’t be surprised if creating a “search” or “searches” controller  
would lead you down the easy path–then you just use the CRUD actions  
for your searches.

Jamie

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 4, 2006, 5:02pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/3 "2006-10-04T17:02:41Z")

</div>

On 10/4/06, Jamie Orchard-Hays [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My first questions are, “what is the functionality you’re trying to  
> create?” and, “do you need a search(es) controller?”
> 
> I’m building an app right now that is a search-type tool. I have three  
> different kinds of resources I use: Query (base class), Search and  
> AdvancedSearch (both inherit from Query). So then I’ve mapped the  
> resources in routes.rb.

Can you give me some more details on this . Particularly, how you  
mapped  
them ?

Likely, you’re not creating models for your searches as I have, but I

> wouldn’t be surprised if creating a “search” or “searches” controller  
> would lead you down the easy path–then you just use the CRUD actions  
> for your searches.

Not sure what you mean creating models for searches ? I’ve done all the  
relationships / associations. What would creating models specific buy  
me  
and what does that entail ?  
I think you might be right about creating a search controller. I was  
hoping  
to keep everything contained for the table to search all in one  
controller.  
Mentally I’m stuck on the idea that search should be restful, but due to  
my  
inexperience I’m not sure I really know why 🙂

Stuart

Jamie

> > suggest a better way ?

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 4, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/4 "2006-10-04T17:44:47Z")

</div>

Here are my routes:

map.resources :queries do |queries|  
queries.resources :terms, :member =\> { :toggle\_op =\> :any }  
end  
map.resources :queries, :member =\> { :add\_term =\> :any }, :collection  
=\> { :clear\_session =\> :delete }  
map.resources :saved\_items, :controller =\> :saved  
map.resources :searches, :advanced\_searches, :documents

Notice you can specify the controller name for a resource. When I  
started this app, I modeled a Search, but not to the database. Later,  
I decided we would be using the db and migrated the architecture to  
use ActiveRecord models. (I’m using Single Table Inheritance with  
Query as the base class. Queries have Terms.)

Anyway, the main thing about Restful routing of resources is that you  
now thing of using Resources instead of creating actions. What’s a  
resource? In your case, a Search is a resource, whether it’s an  
ActiveRecord model, some other model, or just a virtual one. What can  
you do with a resource? You can Create, Update, Delete, and List  
(index) the. New and Edit are just views that allow you to then Create  
or Update. Everything that’s not the basic CRUD becomes an exception,  
so I have :toggle\_op and :add\_term (which arguably, could be Create on  
Term instead, but actually doesn’t work that way in my app).

One thing I’ve learned about Rails–if you follow the conventions,  
programming is a lot more rapid and fun. Once you start fighting  
Rails’ opinions, you have to work a lot harder. In this case, view  
your searches and results as resources. Now you do you architect that  
with Restful Routes?

[http://david.goodlad.ca/articles/2006/08/02/simply\_restful-in-rails-core](http://david.goodlad.ca/articles/2006/08/02/simply_restful-in-rails-core)  
[http://microformats.org/wiki/rest/rails](http://microformats.org/wiki/rest/rails)

Hope this helps.

Jamie

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 4, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/5 "2006-10-04T18:06:51Z")

</div>

On 10/4/06, Jamie Orchard-Hays [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> map.resources :searches, :advanced\_searches, :documents  
> ActiveRecord model, some other model, or just a virtual one. What can  
> with Restful Routes?
> 
> [http://david.goodlad.ca/articles/2006/08/02/simply\_restful-in-rails-core](http://david.goodlad.ca/articles/2006/08/02/simply_restful-in-rails-core)  
> [rest/rails - Microformats Wiki](http://microformats.org/wiki/rest/rails)
> 
> Hope this helps.
> 
> Jamie

Jamie, Thank you. Not sure what your asking me “Now you do you  
architect  
that  
with Restful Routes?” but yes, I think I almost understand about  
setting  
things up as resources. The area that still confuses me, and I posted  
last  
week about this - map.resources :articles, :collection =\> {:recent =\>  
:get}.  
I understand that “recent” is the action, via the get method. What I  
still  
don’t get is how do you set up the :recent ?

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 5, 2006, 2:15am UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/6 "2006-10-05T02:15:34Z")

</div>

On 10/4/06, Jamie Orchard-Hays [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> :path_prefix => "/category/:category_id",
> :name_prefix => "category_"
> 
> ```
> 
> # → GET /categories/7/messages/1
> 
> # has named route “category\_message”

I’m sort of seeing but having a hard time envisioning what an advanced  
search would look like to the user. I can see creating various  
conditions  
within my CRUD to call on, by user, by date, by title, etc. At the same  
time how does one deal with boolean logic in text searches ? I know I’m  
close but could use a little more explanation, not the code part but the  
methodology.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 6, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/7 "2006-10-06T20:49:35Z")

</div>

On 10/4/06, Jamie Orchard-Hays [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jamie  
> I’ve been trying to gain a better understanding of how to use the CRUD  
> actions. I’ve created a Searches controller and a Search model.  
> Anyway, I  
> came across this blog: [http://blog.codahale.com/2006/07/01/](http://blog.codahale.com/2006/07/01/)  
> and in particular this quotation -

“Instead, consider using a Search model. Create a search resource that  
holds  
all the options for the query. Then you can execute it whenever you  
want.  
POST /searches (post data contains query options) would redirect to GET  
/searches/42 or GET /people/?searches=42. You could even make it so that  
searches are unbound until execution, like blocks: GET  
/searches/42?on=people”

My question then does it make sense to structure the controller where  
‘new’  
or ‘create’ where the search is submitted too and index or show is the  
results page ? I’d like to get some comments or feedback since this all  
seems relatively new , some controversy about applying CRUD to search  
abounds as well.

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 7, 2006, 7:47am UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/8 "2006-10-07T07:47:41Z")

</div>

On 10/6/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 10/4/06, Jamie Orchard-Hays [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > My first questions are, “what is the functionality you’re trying to  
> > create?” and, “do you need a search(es) controller?”

Beast uses the standard index method. In fact, I think these 3 all  
call the same action:

[http://beast.caboo.se/posts](http://beast.caboo.se/posts)  
[http://beast.caboo.se/posts?q=rest](http://beast.caboo.se/posts?q=rest)  
[http://beast.caboo.se/users/1/posts](http://beast.caboo.se/users/1/posts)

It’s not really the most elegant solution, but it’s a compact way of  
handling 3 separate actions:

[http://svn.techno-weenie.net/projects/beast/trunk/app/controllers/posts\_controller.rb](http://svn.techno-weenie.net/projects/beast/trunk/app/controllers/posts_controller.rb)

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

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 4, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/asking-another-way-no-rest-actions-in-a-restful-controller/70972/9 "2006-10-04T19:08:59Z")

</div>

typo… “How do you…”

Regarding :recent…

Have a look at the docs for rails edge (you can generate them  
yourself). Here’s an excerpt about custom actions:

Examples:

map.resources :messages, :path\_prefix =\> “/thread/:thread\_id”

# –\> GET /thread/7/messages/1

map.resources :messages, :collection =\> { :rss =\> :get }

# –\> GET /messages;rss (maps to the #rss action)

# also adds a named route called “rss\_messages”

map.resources :messages, :member =\> { :mark =\> :post }

# –\> POST /messages/1;mark (maps to the #mark action)

# also adds a named route called “mark\_message”

map.resources :messages, :new =\> { :preview =\> :post }

# –\> POST /messages/new;preview (maps to the #preview action)

# also adds a named route called “preview\_new\_message”

map.resources :messages, :new =\> { :new =\> :any, :preview =\> :post }

# –\> POST /messages/new;preview (maps to the #preview action)

# also adds a named route called “preview\_new\_message”

# –\> /messages/new can be invoked via any request method

map.resources :messages, :controller =\> “categories”,  
:path\_prefix =\> “/category/:category\_id”,  
:name\_prefix =\> “category\_”

# –\> GET /categories/7/messages/1

# has named route “category\_message”
