{"id":35,"date":"2009-05-04T09:37:15","date_gmt":"2009-05-04T09:37:15","guid":{"rendered":"http:\/\/nicholshayes.myzen.co.uk\/blog\/?p=35"},"modified":"2009-05-04T10:28:16","modified_gmt":"2009-05-04T10:28:16","slug":"using-tempobject-with-search-form","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=35","title":{"rendered":"Using TempObject with a search form"},"content":{"rendered":"<p>Here is how I use <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=31\">TempObject<\/a> with a search form.<\/p>\n<p>I&#8217;ll use an example of creating a form to return balls. In the controller I first create a TempObject with the attributes I want to offer in the search form:<\/p>\n<pre>\r\n@ball_search = TempObject.new(:colour, :size)\r\n<\/pre>\n<p>I can then use standard Rails form helpers to create the search form within a view:<\/p>\n<pre>\r\n<% form_tag do %>\r\n  &lt;p&gt;Colour:  <%= text_field 'ball_search', 'colour' -%>&lt;\/p&gt;\r\n  &lt;p&gt;Size:  <%= text_field 'ball_search', 'size' -%>&lt;\/p&gt;\r\n  &lt;p&gt;<%= submit_tag(\"Search for balls\") -%>&lt;\/p&gt;\r\n<% end %>\r\n<\/pre>\n<p>It is then easy to gather the data passed from the form and load the @ball_search object with it by adding this line into the controller (below the statement creating the object):<\/p>\n<pre>\r\n@ball_search.load_data(params[:ball_search]) if params[:ball_search]\r\n<\/pre>\n<p>The data can then be used to find the balls with these attributes:<\/p>\n<pre>\r\nBall.find(:all, \r\n  :conditions => [\"colour = ? AND size = ?\",\r\n          @ball_search.colour,\r\n          @ball_search.size])\r\n<\/pre>\n<p>This is a simple example, where I&#8217;ve just used text_field form helpers, but it works just as well with the other standard Rails form helper elements. <\/p>\n<p>You can also use @ball_search.attribute_names to create the find conditions:<\/p>\n<pre>\r\nsearch_query = Array.new\r\nsearch_data = Array.new\r\nfor name in @ball_search.attribute_names\r\n  search_query << \"#{namd} = ?\"\r\n  search_data << @ball_search.send(name)\r\nend\r\nBall.find(:all, \r\n  :conditions => [search_query.join(\" AND \"), \r\n          search_data].flatten)\r\n<\/pre>\n<p>With that solution, if another search criteria needs to be added, all that is needed is to add the extra criteria to the original object creation statement and then add the extra form field.<\/p>\n<p>controller:<\/p>\n<pre>\r\n@ball_search = TempObject.new(:colour, :size, :sport)\r\n<\/pre>\n<p>view:<\/p>\n<pre>\r\n<% form_tag do %>\r\n  &lt;p&gt;Colour:  <%= text_field 'ball_search', 'colour' -%>&lt;\/p&gt;\r\n  &lt;p&gt;Size:  <%= text_field 'ball_search', 'size' -%>&lt;\/p&gt;\r\n  &lt;p&gt;Sport:  <%= text_field 'ball_search', 'sport' -%>&lt;\/p&gt;\r\n  &lt;p&gt;<%= submit_tag(\"Search for balls\") -%>&lt;\/p&gt;\r\n<% end %>\r\n<\/pre>\n<p>The code to gather all the data together and populate the find query remains the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is how I use TempObject with a search form. I&#8217;ll use an example of creating a form to return balls. In the controller I first create a TempObject with the attributes I want to offer in the search form: &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=35\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/35"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":15,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":57,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/57"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}