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