Forum: Ruby on Rails Agile Rails (1st edition), depot TDD "exercise"

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-05-06 03:16
(Received via mailing list)
Hash: SHA1

I've been working my way through the Agile book and just completed
the depot sample application. I learned quite a bit along the way.

On page 171 we are led through the creation of a test (that fails)
for new code. The new code (a search function) is left as an exercise
for the reader. Well, I think I solved it but I'm not sure I did it
right (even though it passes the test).

After much experimentation, here's what I did:

1. Edit the depot/app/controller/search_controller.rb file:

class SearchController < ApplicationController

   def search
     @query =[:query])
     @products = Product.find(:all,
                               :conditions => "title like '%#{@query}
%' OR
                                 description like '%#{@query}%'")

     flash[:notice] = "Found #{@products.size} product(s)."


2. cp depot/app/views/layout/store.rhtml depot/app/views/layout/

3. cp depot/app/views/store/index.rhtml depot/app/views/search/

4. Edit depot/app/views/search/search.rhtml
Add in another set of <div> tags around it so we have <div
class="results"> as the outermost one. It now looks like:

<div class="results">
<% @products.each do |product| %>
   <div class="catalogentry">
       <img src="<%= product.image_url %>"/>
       <h3><%= h product.title %></h3>
       <%= product.description %>
       <span class="catalogprice"><%= fmt_dollars(product.price) %></
       <%= link_to 'Add to Cart',
                   {:action   => 'add_to_cart', :id => product },
                   :class => 'addtocart' %><br/>
   <div class="separator">&nbsp;</div>
<% end %>

<%= link_to "Show my cart", :action => "display_cart" %>

5. Change the test so it does "assert_template 'search/search'"
instead of "assert_template 'search/results'".

6. There is no step six.

Was there a better or easier way of accomplishing this task?

Also, why did the book want to test "assert_template 'search/
results'"? That seems like an error to me. The default view for a
controller's method (action) has the same name as the action, right?
Unless you do a "redirect_to :action => 'results'" in which case the
"assert_response :success" would now fail because we are redirecting
to a page with a different name. This really confused me and caused
me to flail around much longer than necessary to solve this problem.

Or is the test correct and I can have an action named "search"
rendered by a view named "results"?


Version: GnuPG v1.4.3 (Darwin)

Tony P. (Guest)
on 2006-05-06 03:28
(Received via mailing list)
OK, so I had to do a couple of more installs of Rails on two new servers
today.  I realized that my notes were incomplete because I made a couple
of mistakes in the process.  So, I've updated my wiki page with more
infomation on what I did to get this to work.  These troubleshooting
that I added should be a fairly good guide to correcting some of the
common FastCGI and Apache problems that you can encounter on any distro.

This topic is locked and can not be replied to.