# Depot App Who Bought Enhancements

**URL:** <https://www.ruby-forum.com/t/depot-app-who-bought-enhancements/165720>\
**Category:** Rails\
**Created:** [April 16, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/depot-app-who-bought-enhancements/165720 "2009-04-16T18:45:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eengnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@eengnerd](https://www.ruby-forum.com/u/eengnerd)\
**Post date:** [April 16, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/depot-app-who-bought-enhancements/165720/1 "2009-04-16T18:45:34Z")

</div>

I’ve completed the chapter on Internationalization successfully, but  
found out just recently that the “Who Bought” function produces an  
error page that looks very ugly when someone enters a product ID that  
does not exist. My preference would be to design the app to have a  
list of hyperlinks presented when you visit the URL,  
[http://localhost:3000/info/](http://localhost:3000/info/)  
that describes the list of “Info Functions” (eg “Who Bought” for  
starters). Then when it takes you there,  
[http://localhost:3000/info/who\_bought/](http://localhost:3000/info/who_bought/)  
you would see a list of Book Titles as hyperlinks so that when you  
click on a book title, it redirects you to the appropriate URL for  
seeing a list of customers “who bought” that book. In this way I  
could avoid the user’s temptation to use “non-existent” product ID’s  
before it becomes a problem. But I have no idea how to go about  
creating these features. I’m open to some ideas.

Arthur

---

<div class="post-metadata">

**Author:** ![eengnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@eengnerd](https://www.ruby-forum.com/u/eengnerd)\
**Post date:** [April 16, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/depot-app-who-bought-enhancements/165720/2 "2009-04-16T19:19:22Z")

</div>

eengnerd wrote:

> I’ve completed the chapter on Internationalization successfully, but  
> found out just recently that the “Who Bought” function produces an  
> error page that looks very ugly when someone enters a product ID that  
> does not exist. My preference would be to design the app to have a  
> list of hyperlinks presented when you visit the URL,  
> [http://localhost:3000/info/](http://localhost:3000/info/)  
> that describes the list of “Info Functions” (eg “Who Bought” for  
> starters). Then when it takes you there,  
> [http://localhost:3000/info/who\_bought/](http://localhost:3000/info/who_bought/)  
> you would see a list of Book Titles as hyperlinks so that when you  
> click on a book title, it redirects you to the appropriate URL for  
> seeing a list of customers “who bought” that book. In this way I  
> could avoid the user’s temptation to use “non-existent” product ID’s  
> before it becomes a problem. But I have no idea how to go about  
> creating these features. I’m open to some ideas.
> 
> Arthur

Sounds like the hard part is done, now it’s just coding…

So, where does …info/ go to?

You’ll need a view for that (info.html.erb?), and you’ll have to get  
there somehow (think routes) via some controller (which to use… or  
create?)

The list of book titles for ‘Who Bought’ smells an awful lot like a  
generic book ‘index’ view, with maybe a twist on the rendering in the  
erb.

If a book knows who bought it, listing the people who bought a book  
smells like an adaptation of a book ‘show’ view.

Just get that info page working, then extend from there.

---

<div class="post-metadata">

**Author:** ![eengnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@eengnerd](https://www.ruby-forum.com/u/eengnerd)\
**Post date:** [April 16, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/depot-app-who-bought-enhancements/165720/3 "2009-04-16T19:50:49Z")

</div>

To keep things simple for the moment, I created a file in /home/arthur/  
RubySites/depot/app/views/info with a filename of index.html.erb and  
put the following code into it:

### Info Functions

[Who Bought](http://localhost:3000/info/who_bought)

Of course when you click this hyperlink, the file who\_bought.html.erb  
responds with:

ActiveRecord::RecordNotFound in InfoController#who\_bought

Couldn’t find Product without an ID

So, my guess would be that I need to “test” for the condition where  
the ID is missing and write some code in that file that loops through  
the products table and creates hyperlinks such as:

[Pragmatic  
Project Automation](http://localhost:3000/info/who_bought/1.html)

for each book title in the database.

I have to think about the syntax for all of that…any ideas?

Arthur

On Apr 16, 10:19 am, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eengnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@eengnerd](https://www.ruby-forum.com/u/eengnerd)\
**Post date:** [April 17, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/depot-app-who-bought-enhancements/165720/4 "2009-04-17T19:37:40Z")

</div>

I think I’m pretty close now. There are 3 files involved:

app/contollers/info\_controller.rb which contains:

class InfoController \< ApplicationController  
def who\_bought  
if params[:id] == nil then  
@products = Product.all  
respond\_to do |format|  
format.html  
format.xml { render :xml =\> @products }  
end  
else  
@product = Product.find(params[:id])  
@orders = @product.orders  
respond\_to do |format|  
format.html  
format.xml { render :layout =\> false , :xml =\> @product.to\_xml  
(:include =\> :orders) }  
end  
end  
end  
end

app/views/info/who\_bought.html.erb which contains:

if @products != nil then  
\<% for product in @products %\>  
\<a href=“[http://localhost:3000/info/who\_bought/](http://localhost:3000/info/who_bought/)”+\<%=h product.id %\>  
+“.html”\>\<%=h product.title %\>  
\<% end %\>  
else

### People Who Bought \<%= @product.title %\>

 \<% for order in @orders -%\> - \<%= mail\_to order.email, order.name %\> 
 \<% end -%\> 
end

app/views/info/index.html.erb which contains:

### Info Functions

[Who Bought](http://localhost:3000/info/who_bought)

The problem is that when I visit [http://localhost:3000/info/who\_bought/](http://localhost:3000/info/who_bought/)

I get the following error screen:

NoMethodError in Info#who\_bought

Showing app/views/info/who\_bought.html.erb where line #6 raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.title

Extracted source (around line #6):

3: \<a href=“[http://localhost:3000/info/who\_bought/](http://localhost:3000/info/who_bought/)”+\<%=h  
product.id %\>+“.html”\>\<%=h product.title %\>  
4: \<% end %\>  
5: else  
6:

### People Who Bought \<%= @product.title %\>
  
7: 
  
8: \<% for order in @orders -%\>  
9: -
