rubyonrailsin

A Ruby and Rails talk

Friday, April 2, 2010


[Rails] Re: Trying to create search filter persistence using SearchLogic

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

dburges wrote:
> I have just set up a search form on my index using SearchLogic- it
> works great. However, since there are several search parameters in the
> form I want the results of the most recent search to remain in effect
> until the user does a new search. I can't figure out how to do this!
> I've tried a few things with global and session variables but just
> can't get it working.
>
> Any advice would be greatly appreciated!

The previous form values you want for re-initializing the form are
passed into your controller method in the params object. If you have
built your form around an object called @search like this:

form_for @search do |f|

you just need to initialize @search with the search values in params, in
the controller method (perhaps Controller#index), like this:

@search = Model.search(params[:search])

If you have fields that are not part of Model that you want to include
in the search and have those initialized to, define a named_scope in
Model, and use that named_scope in the form for @search. Then it will
also be part of the params search values and get initialized along with
the other, automatically generated named scopes that correspond to the
model fields.

For example, to add a check box that limits the search to rows with a
certain condition, you could define a named scope in Model.rb like this:

named_scope :my_condition, :conditions => "your sql condition here"

Then in the view form, you would include:
<%= f.label :my_condition, "Check to apply my condition" %>
<%= f.check_box :my_condition %>
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive