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…
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…
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).
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:
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.
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:
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.
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.
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.
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:
user does a search
user clicks a result and views the detail page
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.
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.