# REST search parameters

**URL:** <https://www.ruby-forum.com/t/rest-search-parameters/143348>\
**Category:** Rails\
**Created:** [July 17, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348 "2008-07-17T16:35:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/1 "2008-07-17T16:35:42Z")

</div>

Hi,

I’m trying to implement a search form in my index view. Since the app is  
designed REST, I need to submit the form with “get” method. This way,  
all the search parameters get passed in the url.

Is there a way to get this to work by using the post method without  
having to create a seperate controller for search actions? Maybe hide  
the search parameters as far as this is possible?

It’s a simple search on a date range, so I don’t need anything fance,  
but I do like it to be coded as close to rest as possible.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/2 "2008-07-17T16:55:29Z")

</div>

Hey Michael…

The way I go about this is by having parameters set in my “find”  
method of the index controller action… for example…

Client.find :all, :conditions =\> [ " first\_name like ? AND last\_name  
like ? ", params[:first\_name], params[:last\_name] ]

And this way, a load to index.rhtml will load everything, since  
there’s no “search” but if you pass one of those params, it’ll add in  
those conditions…

Obviously modify this how you need

On Jul 17, 10:35 am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/3 "2008-07-17T16:59:27Z")

</div>

sw0rdfish wrote:

> Hey Michael…
> 
> The way I go about this is by having parameters set in my “find”  
> method of the index controller action… for example…
> 
> Client.find :all, :conditions =\> [ " first\_name like ? AND last\_name  
> like ? ", params[:first\_name], params[:last\_name] ]
> 
> And this way, a load to index.rhtml will load everything, since  
> there’s no “search” but if you pass one of those params, it’ll add in  
> those conditions…
> 
> Obviously modify this how you need
> 
> On Jul 17, 10:35ï¿½am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi sw0rdfish. I know how to implement the search. My main consern is the  
fact that the search parameters of my form are visible in the address  
bar of the browser (since the data needs to be send with “get”  
parameter, if I use the “post” parameter, I get directed to the create  
action becouse of the REST implementation).

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/4 "2008-07-17T16:59:36Z")

</div>

On Jul 17, 9:35 am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It’s a simple search on a date range, so I don’t need anything fance,  
> but I do like it to be coded as close to rest as possible.
> 
> Thank you in advance

You can have a form use get instead of post, if you prefer, by  
appending :method =\> :get inside your form\_for clause. The search  
parameters will appear in the url and Rails will parse them into the  
params[] hash for you:

\<% form\_for :products, { :method =\> :get } do |f| %\>  
\<%= text\_box :q %\>  
\<%= f.submit “Search” %\>

My syntax might be off, just doing this off the top of my head, but  
the idea is you target your index action (here I’ve used a  
ProductsController just as an example).

Inside your index action, you can see if params[:q] is present, and if  
so filter your usual view based on the given value.

Hope this helps?

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

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/5 "2008-07-17T17:02:08Z")

</div>

Jeff C. wrote:

> On Jul 17, 9:35ï¿½am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > It’s a simple search on a date range, so I don’t need anything fance,  
> > but I do like it to be coded as close to rest as possible.
> > 
> > Thank you in advance
> 
> You can have a form use get instead of post, if you prefer, by  
> appending :method =\> :get inside your form\_for clause. The search  
> parameters will appear in the url and Rails will parse them into the  
> params[] hash for you:
> 
> \<% form\_for :products, { :method =\> :get } do |f| %\>  
> \<%= text\_box :q %\>  
> \<%= f.submit “Search” %\>
> 
> My syntax might be off, just doing this off the top of my head, but  
> the idea is you target your index action (here I’ve used a  
> ProductsController just as an example).
> 
> Inside your index action, you can see if params[:q] is present, and if  
> so filter your usual view based on the given value.
> 
> Hope this helps?
> 
> Jeff  
> [purpleworkshops.com](http://purpleworkshops.com)  
> [softiesonrails.com](http://softiesonrails.com)

Hi Jeff, thank you for your reply. It looks like I wasn’t clear enough  
about my “problem”.  
I know that it is becouse of the “get” method that my search values get  
shown in the address bar.  
The problem is, if I use a post, I get directed to the create action,  
becouse ror interpretes this as a create.

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/6 "2008-07-17T19:12:26Z")

</div>

Jeff C. wrote:

> On Jul 17, 10:02ï¿½am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi Jeff, thank you for your reply. It looks like I wasn’t clear enough  
> > about my “problem”.  
> > I know that it is becouse of the “get” method that my search values get  
> > shown in the address bar.  
> > The problem is, if I use a post, I get directed to the create action,  
> > becouse ror interpretes this as a create.
> 
> Ah! Sorry if I misunderstood.
> 
> I suppose you can still allow it to hit the create action of your  
> existing controller and check for the existence of certain params to  
> detect if it’s “really” a search (and then do a render :action =\>  
> ‘index’ instead of a redirect after you gather up the filtered data  
> you want to show), but I think that might get messy after a while.
> 
> Jeff

Hi Jeff,

np, my description wasn’t that great eather. Maybe another solution may  
be to use an Ajax search instead.

Does anyone know an example of a restful ajax search? I have googled on  
this, but it looks like REST description for search actions hasn’t been  
fully determend.

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/7 "2008-07-17T20:21:49Z")

</div>

It’s simple, really…  
if you want to follow REST then you use GET for your searches. POST has  
other limitations too, like the “repost / expiry” problem:

1. user does a search
2. user clicks a result and views the detail page
3. user presses back and the browser says “page expired” or “please  
repost  
params”

There’s nothing generally wrong with passing parameters in the URL. It  
makes it really easy to save searches, to pass them to sompne else,  
even.

In fact, here’s a good example -  
[http://www.google.com/search?hl=en&q=“restful+searching”+%2B+rails](http://www.google.com/search?hl=en&q=%22restful+searching%22+%2B+rails)

If you choose to ignore all of this, then you either need to make a new  
action

map.resources :projects, :collection =\>{:search =\> :post}

or make your own search controller where the ‘create’ method would be  
creating a new search, though I really dislike that approach. GET for  
search  
results is much more user-friendly - people do use the back button.

On Thu, Jul 17, 2008 at 12:12 PM, Michael R. \<

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 17, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/rest-search-parameters/143348/8 "2008-07-17T17:30:43Z")

</div>

On Jul 17, 10:02 am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Jeff, thank you for your reply. It looks like I wasn’t clear enough  
> about my “problem”.  
> I know that it is becouse of the “get” method that my search values get  
> shown in the address bar.  
> The problem is, if I use a post, I get directed to the create action,  
> becouse ror interpretes this as a create.

Ah! Sorry if I misunderstood.

I suppose you can still allow it to hit the create action of your  
existing controller and check for the existence of certain params to  
detect if it’s “really” a search (and then do a render :action =\>  
‘index’ instead of a redirect after you gather up the filtered data  
you want to show), but I think that might get messy after a while.

Jeff
