Submitting only populated attributes from a form?

I have a form where a user can perform a search on many attributes. This
is fine as long as the user specifies each attribute. But if one is left
blank then the submit blows up. Actually it’s the find method in the
controller that blows up. Is there a way to submit a form where only the
populated attributes are send to the controller? If this is not possible
I’ll have to put some default value for each attribute, like Null. Is
there a way in the find condition to ignore these?

Here is the conditions part of my controller code.
person.find (:all, :conditions => [“family = :family and gender =
:gender and etc. etc.”, params])

-Anthony

Anthony W. wrote:

I have a form where a user can perform a search on many attributes. This
is fine as long as the user specifies each attribute. But if one is left
blank then the submit blows up. Actually it’s the find method in the
controller that blows up. Is there a way to submit a form where only the
populated attributes are send to the controller? If this is not possible
I’ll have to put some default value for each attribute, like Null. Is
there a way in the find condition to ignore these?

Here is the conditions part of my controller code.
person.find (:all, :conditions => [“family = :family and gender =
:gender and etc. etc.”, params])

-Anthony

You can’t prevent the browser form sending all form fields. The right
way to do this is make sure you are only searching on fields that the
user filled in by stripping the bad ones out first.

Here’s how I would do it:

def search
#define a list of fields that are searchable
search_fields = [
:family,
:name,
:gender,
]

#remove fields form the list which were not filled in by user
search_fields.reject! do |field|
  params[field].nil? || params[field].empty?
end

#generate array of condition fragments for the leftover fields
conditions = search_fields.collect do |field|
  "#{field} = :#{field}"
end

#join array with AND resulting in a string
conditions = conditions.join(' AND ')

#do find
@people = Peron.find(:all, :conditions => [conditions, params])

end

Alex W. wrote:

You can’t prevent the browser form sending all form fields. The right
way to do this is make sure you are only searching on fields that the
user filled in by stripping the bad ones out first.

That works! Thanks for the help.

-Anthony