Forum: Ruby on Rails recipes/categories to books/authors but listing doesn't work

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.
Philip R. (Guest)
on 2006-01-05 06:43
(Received via mailing list)

I have gone through the cookbook example:

and everything worked fine an a FC4/Postgres setup.

I then tried to reproduce this success with something that would be
useful to me - a library db using books & authors tables.  The author
list works but NOT the book list (unless I take line #17 out).

I get this message:

Showing app/views/book/list.rhtml where line #17 raised:

You have a nil object when you didn't expect it!
The error occured while evaluating nil.last_name

Extracted source (around line #17):

14:  <% @books.each do |book| %>
15:   <tr>
16:    <td><%= link_to book.title, :action => "show", :id =>
17:    <td><%= %></td>
18:   </tr>
19:  <% end %>
20: </table>

but as far as I can tell I have done the right things in the
controllers, models and views:


    @authors = Author.find  :all,
                            :order => 'last_name, first_name'


    @books = Book.find  :all,
                        :order => 'title, back'


    has_many :books


    belongs_to :author


<table border="0">
  <td width="60%"><p align="left"><i><b>Book</b></i></td>
  <td width="20%"><p align="left"><i><b>Author</b></i></td>

 <% @books.each do |book| %>
   <td><%= link_to book.title, :action => "show", :id => %></td>
   <td><%= %></td>
 <% end %>

authors table:

 id         | integer               | not null default
 last_name  | character varying(20) |
 first_name | character varying(20) |
 other_name | character varying(20) |

books table:

 id        | integer               | not null default
 media     | character(2)          | not null
 title     | character varying(50) | not null
 year      | integer               | not null
 cat       | character(1)          | not null
 back      | character(1)          |
 own       | character(1)          |
 author_id | integer

I have been battling with this for ages but can't see any bugs left -
any suggestions?



Philip R.

Pricom Pty Limited  (ACN 003 252 275  ABN 91 003 252 275)
GPO Box 3411
Sydney NSW      2001
Mobile:  +61:(0)411-185-652
Fax:     +61:(0)2-8221-9599
E-mail:  removed_email_address@domain.invalid
Damon C. (Guest)
on 2006-01-05 08:32
(Received via mailing list)
Philip R. <phil@...> writes:

> I have been battling with this for ages but can't see any bugs left -
> any suggestions?

It would appear that you have a book with no author.

replace the stuff in your view with


and see if any book has a nil author.

This topic is locked and can not be replied to.