Add customer searching capability
authorIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 05:23:20 +0000 (21:23 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 05:23:20 +0000 (21:23 -0800)
This allows simple customer searching by name.

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
app/controllers/customer_controller.rb
app/views/customer/searchbyname.rhtml [new file with mode: 0644]
app/views/customer/searchresults.rhtml [new file with mode: 0644]

index 6f7c8ee..d741644 100644 (file)
@@ -48,4 +48,13 @@ class CustomerController < ApplicationController
     Customer.find(params[:id]).destroy
     redirect_to :action => 'list'
   end
+
+  def searchbyname
+    render :action => 'searchbyname'
+  end
+
+  def searchresults
+    query = params[:q]
+    @customers = Customer.find(:all, :conditions => ["name = ?", query])
+  end
 end
diff --git a/app/views/customer/searchbyname.rhtml b/app/views/customer/searchbyname.rhtml
new file mode 100644 (file)
index 0000000..7b17ec1
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>Search for a Customer by Name</h1>
+
+<%= start_form_tag :action => 'searchresults'%>
+<%= text_field 'q', nil  %>
+  <%= submit_tag 'Search' %></form>
+<%= end_form_tag %>
+
+<br />
diff --git a/app/views/customer/searchresults.rhtml b/app/views/customer/searchresults.rhtml
new file mode 100644 (file)
index 0000000..a6488ce
--- /dev/null
@@ -0,0 +1,26 @@
+<h1>Search Results</h1>
+
+<% if @customers.empty? %>
+<p>Sorry, there were no results</p>
+<% else %>
+<table>
+  <tr>
+  <th>Customer ID</th>
+  <% for column in Customer.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+
+<% for customer in @customers %>
+  <tr>
+  <td><%=h customer.id %></td>
+  <% for column in Customer.content_columns %>
+    <td><%=h customer.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => customer %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => customer %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => customer }, :confirm => 'Are you sure?', :post => true %></td>
+  </tr>
+<% end %>
+</table>
+<% end %>