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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Anthony Walsh 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]) > > -AnthonyYou 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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Wayne 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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---