I’ve used an approach frequently in the past where I create a method
in the model that when passed parameters from a selection form returns
a modified result set.
As an example the routine in the model looks like this;
def self.find_by_search(params) (shortened for easy reading)
where = []
unless params[:agency].blank?
where << “agency_id = :agency”
end
if where.empty?
find(:all,
:order => params[:order])
else
find(:all,
:conditions => [where.join(" AND "), params],
:order => params[:order])
end
end
Then the controller uses this model/method to modify the result set
based on the user’s entries.
def ecase_search
@title = “Search Cases”
return if params[:commit].nil?
@ecase = @account.ecases.find_by_search(params)
@ecases = @ecase.paginate :per_page => 8,
:page => params[:page]
render(:template => “evidence/ecases/index”)
end
The search form that is supposed to pass the params back to the
controller action looks like this;
<% form_tag( ecase_search_path, :method => “get”) do %> (shortened
for easier reading)
<%= "Search Field" %> | <%= "Search Value" %> |
---|---|
<%= "Agency Code" %> | <% @agencies = @account.agencies.find(:all) %> <% @agencies < '', :description => 'All') %><%= select_tag(:agency, options_for_select(@agencies.collect { |c| [ c.description, c.id ] }), :style => "width:150px", :id => :indexselect) %> |
<%= "Order By" %> | <%= select_tag(:order, options_for_select(@selections), :style => "width:150px", :id => :indexselect) %> |
<%= submit_tag "Find Results", :class => "submit" %> |
The routing in routes.rb looks like this;
map.ecase_search ‘ecase_search’, :controller => ‘evidence/
ecases’, :action => ‘ecase_search’
The error I get when I try this approach is;
ActionController::MethodNotAllowed
Only get, head, post, put, and delete requests are allowed.
This approach worked wonderfully in 2.0 but now fails in 2.1. I had a
similar problem in 2.1 when I found that nested tables had to be
called differently in the form_for such as (<% form_for([@account,
@agency]) do |form| % )
Any thoughts are greatly appreciated.
Kathleen