# RESTful resource with multiple view representations

**URL:** <https://www.ruby-forum.com/t/restful-resource-with-multiple-view-representations/151965>\
**Category:** Rails\
**Created:** [October 28, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/restful-resource-with-multiple-view-representations/151965 "2008-10-28T14:43:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [October 28, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/restful-resource-with-multiple-view-representations/151965/1 "2008-10-28T14:43:41Z")

</div>

Hi,

Could somewhat offer guidance on this situation.

Say I have a Product model and an associated resource reference  
defined:

map.resources :products, :collection =\> { :search =\> :get }

I need to access the search action but have a different view  
(generally RJS) rendered depending on the context of the call.

For example, if called from an order assembly page I might need the  
resulting product collection displayed with add to basket links.  
Whereas on another page I might need them rendered with add to  
purchase order links.

I see to obvious solutions:

1. Define multiple actions within a single resource. They would  
essentially have the same code but would call different RJS view code  
to render the result.

=\> :get, :purchase\_order\_search =\> :get }

1. Have multiple resources such as as:

=\> :get }  
map.resources :purchase\_order\_products, :collection =\>  
{ :search =\> :get }

If going down route 2 I’d probably just repurpose the index action to  
check for a search\_string param to narrow the results returned.

Both seem less than perfect, although I’m tended towards solution 1.  
Although I’ve only highlighted two view contexts there could be more,  
I would suggest 3 or 4 could be common. How would you handle this  
situation?

Thanks, Andrew

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [October 28, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/restful-resource-with-multiple-view-representations/151965/2 "2008-10-28T15:01:20Z")

</div>

On Oct 28, 8:43 am, Andrew E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (generally RJS) rendered depending on the context of the call.  
> to render the result.
> 
> If going down route 2 I’d probably just repurpose the index action to  
> check for a search\_string param to narrow the results returned.
> 
> Both seem less than perfect, although I’m tended towards solution 1.  
> Although I’ve only highlighted two view contexts there could be more,  
> I would suggest 3 or 4 could be common. How would you handle this  
> situation?
> 
> Thanks, Andrew

First off, what about using the same search action for each, and using  
query parameters to specify the desired behavior? That way you won’t  
need a new custom action every time you need a new “flavor” of search  
call. It’s perfectly RESTful to append any query parameters you need  
to your actions (/products/search?mode=purchase, or whatever).

However, maybe you really need a separate controller entirely, like a  
ProductSearchesController, whose index action can take whatever  
parameters you think are appropriate. That might help encapsulate all  
of your searching functionality and keep the ProductsController  
restful and clean.

Just some ideas…

Jeff

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

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [October 28, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/restful-resource-with-multiple-view-representations/151965/3 "2008-10-28T15:20:41Z")

</div>

Thanks, I think I looked at something similar to a mode/context param  
previously but seemed to drift away. I might have another look.

In this scenario are you suggesting sticking the conditional logic  
against the mode param in the action RJS view?

@display\_context = params[:context]

respond\_to do |format|  
format.js  
end

Is there a way to specify the specific RJS file in the action?

Concept (format.js lines not necessarily valid, just to show  
intention):

display\_context = params[:context]

respond\_to do |format|  
if display\_context == “sales”  
format.js { :render =\> ‘sales\_products’ }  
if display\_context == “purchase”  
format.js { :render =\> ‘purchase\_products’ }  
else  
format.js { :render =\> ‘products’ }  
end  
end

---

<div class="post-metadata">

**Author:** ![reynar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@reynar](https://www.ruby-forum.com/u/reynar)\
**Post date:** [November 15, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/restful-resource-with-multiple-view-representations/151965/4 "2008-11-15T22:53:24Z")

</div>

On Oct 28, 8:43 am, Andrew E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (generally RJS) rendered depending on the context of the call.  
> to render the result.
> 
> If going down route 2 I’d probably just repurpose the index action to  
> check for a search\_string param to narrow the results returned.
> 
> Both seem less than perfect, although I’m tended towards solution 1.  
> Although I’veonlyhighlighted two view contexts there could be more,  
> I would suggest 3 or 4 could be common. How would you handle this  
> situation?
> 
> Thanks, Andrew

G’day Andrew. I agree with Jeff that you should refactor searching out  
of ProductsController.

I was in a similar situation to yours, where I needed to search for  
[real estate] properties. At first, I created #filter in the Property  
model, as well as #filter in PropertiesController. However, as time  
went on, this became cumbersome, and filled up the Property model with  
many methods that were only loosely related. To make life simpler, I  
moved #filter from Property and PropertiesController into a new model,  
called PropertyFilter.

Now when I want to search for properties with varying parameters, I  
simply create a PropertyFilter instance, and let that work its magic  
to perform the search.

If you decide to do this, I recommend having your new model inherit  
from ActiveRecord::BaseWithoutTable :  
[http://agilewebdevelopment.com/plugins/activerecord\_base\_without\_table](http://agilewebdevelopment.com/plugins/activerecord_base_without_table)  
It gives the model all of the fancy ActiveRecord methods (like #find,  
#validates\_\*, etc) without the need for a database table behind it.

I hope that helps. Cheers,  
Nick
