REST search parameters

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

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

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 softiesonrails.com

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 Rigart <rails-mailing-l...@andreas-s.net>

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).

Jeff Cohen wrote:

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

Jeff Cohen wrote:

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=%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.