small medium large xlarge

28 Jun 2013, 11:01
zofia sunderland (2 posts)


I use the fourth edition of your book and when i put this command in the terminal:

curl –silent http://localhost:3000/products/3/who_bought.atom

i get very long printout where i find, among other things:

    NoMethodError in

<div id="container">
    Showing <i>/home/bartek/Pulpit/CQ_RoR/work/depot/app/views/products/who_bought.atom.builder</i> where line <b>#2</b> raised:

undefined method `title' for nil:NilClass ~~~ ~~~

I did all i should according to the chapter and i dont know what is going on. Please help.


29 Jun 2013, 12:27
Sam Ruby (625 posts)

Line 2 reads:

feed.title "Who bought #{@product.title}"

So it looks like @product is nil. It is set in the controller thus:

@product = Product.find(params[:id])

So it looke like you don’t have a product with an id of 3? Try going to http://localhost:3000/products and clicking on one of the show links and observing the id in the url.

01 Jul 2013, 10:07
zofia sunderland (2 posts)

@product is set in the controller’s method who_bought and all the ids in url after clicking show are correct (I checked all three products)…

02 Jul 2013, 00:51
Sam Ruby (625 posts)

Automated regression tests for this chapter are posted online.

Please double check your code for typos. Instance variables don’t need to be declared and default to nil, so if either the assignment or the reference to @product is mispelled, the result will be exactly as you describe.

17 Feb 2015, 13:40
Taras Sirota (1 post)

To fix this issue, you just need to add the text “, :who_bought” to the file products_controller.rb like this:

class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy, :who_bought]

Bingo! It works! )

You must be logged in to comment