Hi, Just noticed that there have been some more posts to this thread. Some time ago now, I played with different ways of handling controls on an index view. I have been developing an approach along the way. I don't have all the details at my fingertips at the moment, but in general, I went along the following lines:
Firstly, the simplest approach is to handle your requests with Get. Then to retain values of controls such as check box values, or select boxes etc, you just need to ensure that the value of the control is defined by the params hash. My first solution was for a radio button and worked like this. In the controller action @search_on = params[:search_on] || 'a' # gets the value or sets the default In the form Search Name: <%= radio_button_tag('search_on','n',@search_on=='n') %> Search All Fields: <%= radio_button_tag('search_on','a',@search_on=='a') %> (You don't need to assign search in the controller, it can all be easily done from params in the view) This is ok for the simple requirements, but I found mapping the fields into searches can become really tedious. The next approach I took (which may not be to everyone's taste) is to build my search criteria into a new object based on the model being searched. In this way, the search fields are available for display in a form for the model itself, which makes handling the fields really easy. eg. The VIEW - (In haml) - form_for :quote, @filter, :html=>{:method=>:get, :id=>'filter'} do | form| %b Filter By: Company = text_field_with_auto_complete :quote, :name, {:value=>@filter.name, :size=>20} %label Created By - opt=(((Quote.find :all, :select=>'distinct created_by').map {|q| q.created_by}).unshift('')) = form.select(:created_by, opt) Status - opt=(((Quote.find :all, :select=>'distinct status').map {|q| q.status}).unshift ('')) = form.select(:status, opt) = submit_tag 'Search' In the CONTROLLER index action: if params[:quote] @filter=Quote.new(params[:quote]) session[:quote_filte...@filter elsif session[:quote_filter] @filter=session[:quote_filter] else @filter=Quote.new(:status=>'', :converted_to_sale=>'false') end @quotes = @filter.search params[:page] @filter is a new record of the quote model that is used to create the form in the view. I use the params[:quote] if there is one since this has come from the view and is the new search criteria. I save the filter to the session If there are no params, then I get the filter from the session so that the last used search is reinstated In the Quote model, I have a method called search which uses the params to build the search. Here is another form using observers to eliminate the submit button. - form_for :supply_order, @filter, :url=>'/supply_orders/index', | :html=>{:name=>'filter_form', :id=>'filter_form'} do |form| | %b Filter By: Order From = form.collection_select :supplier_id, Contact.suppliers, :id, :name, {:include_blank=>true} %label Ordered By: =form.select(:ordered_by,SupplyOrder.ordered_by_list) = ' - ' %b Complete: = form.check_box :complete,{}, checked_value = "1", unchecked_value = "0" = observe_field 'supply_order_supplier_id', | :url=>'/supply_orders/index', | :before => "$ ("+"'filter_form'"+").className='dirty';" , | :with=>"'supply_order[supplier_id]=' + value" | - = observe_field 'supply_order_ordered_by', | :url=>'/supply_orders/index', | :before => "$ ("+"'filter_form'"+").className='dirty';" , | :with=>"'supply_order[ordered_by]=' + value" | OK so this works for search criteria that exist as fields in the model, but what if you have one that isn't in the model. For this situation, I simply add an attr_accessor to the model for that search field. I know purists will cringe at using the model in this way, but it does result in a neat and consistent solution I hope this may give you some ideas. Finally, I have now developed an abstracted solution which can be slotted into any resource. It saves the search in a database model called Search. I save an entry for each user, serailizing the search fields into the database and also saving the page number and page size. All searches are ajax enabled, and I even allow the page size to be directly edited in the view so you can set your own preference. The search is handled in the index action by (for model Thing): @search=Search.set_search(user,Thing,params) @filt...@search.filter @thin...@search.do_search Search.set gets or creates an entry in the search table for the model/ user. @filter gets the fields from the current or new search by calling @search.filter. I have arranged it so that it works using ajax, updating only one search field at a time. @filter is used to display the form in the view @filter.do search handles pagination and calls a search method in the model to be searched (which needs to be created individually for each model). If anyone is interested in more detail, then I could put some notes together. It is the sort of thing that might make a plugin, but I don't have experience in that at the moment, and the whole thing still needs a bit of cleaning up. If I can get it cleaned up in my apps, I may try to put some time aside to make it a plugin. Tonypm --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---