Game.find(params[:id]).destroy
redirect_to :action => 'list'
end
+
+ def searchbyname
+ render :action => 'searchbyname'
+ end
+
+ def searchresults
+ query = params[:q]
+ @games = Game.find(:all, :conditions => ["title = ?", query])
+ end
end
belongs_to :customer
belongs_to :rentable
+ # Make sure the user typed in the ids
validates_presence_of :customer_id
validates_presence_of :rentable_id
+ # Make sure the user typed in numbers
+ validates_numericality_of :customer_id
+ validates_numericality_of :rentable_id
+
+ # Make sure this rentable was not already checked out
validates_uniqueness_of :rentable_id
+ # Make sure the associated rentable and customer are valid and exist
validates_associated :customer
validates_associated :rentable
+
+ protected
+ def validate
+ errors.add(:customer_id, "does not exist is the database") if customer.nil?
+ errors.add(:rentable_id, "does not exist in the database") if rentable.nil?
+ end
end
class Game < Rentable
validates_presence_of :game_genre
validates_presence_of :platform
+
+ def checkedout?
+ return Coitem.find_by_id(self.id) ? true : false
+ end
end
<%= error_messages_for 'coitem' %>
<!--[form:coitem]-->
-<p><label for="coitem_customer_id">Customer</label><br/>
-<%= select 'coitem', 'customer_id', Customer.find(:all).collect {|c| [c.name.to_s, c.id] } %></p>
+<p><label for="coitem_customer_id">Customer ID</label><br/>
+<%= text_field 'coitem', 'customer_id' %></p>
-<p><label for="coitem_rentable_id">Rentable</label><br/>
-<%= select 'coitem', 'rentable_id', Rentable.find(:all).collect {|r| [r.title.to_s, r.id] } %></p>
+<p><label for="coitem_rentable_id">Rentable ID</label><br/>
+<%= text_field 'coitem', 'rentable_id' %></p>
<p><label for="coitem_out_date">Out date</label><br/>
<%= date_select 'coitem', 'out_date' %></p>
<table>
<tr>
+ <th>Game ID</th>
+ <th>Checked Out</th>
<% for column in Game.content_columns %>
<th><%= column.human_name %></th>
<% end %>
<% for game in @games %>
<tr>
+ <td><%=h game.id %></td>
+ <td><%=h game.checkedout? %></td>
<% for column in Game.content_columns %>
<td><%=h game.send(column.name) %></td>
<% end %>
--- /dev/null
+<h1>Search for a Game by Name</h1>
+
+<%= start_form_tag :action => 'searchresults'%>
+<%= text_field 'q', nil %>
+ <%= submit_tag 'Search' %></form>
+<%= end_form_tag %>
+
+<br />
--- /dev/null
+<h1>Search Results</h1>
+
+<% if @games.empty? %>
+<p>Sorry, there were no results</p>
+<% else %>
+<table>
+ <tr>
+ <th>Game ID</th>
+ <th>Checked Out</th>
+ <% for column in Game.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for game in @games %>
+ <tr>
+ <td><%=h game.id %></td>
+ <td><%=h game.checkedout? %></td>
+ <% for column in Game.content_columns %>
+ <td><%=h game.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => game %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => game %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => game }, :confirm => 'Are you sure?', :post => true %></td>
+ </tr>
+<% end %>
+</table>
+<% end %>